通过数组中的多个字符串进行角度过滤
Angular filter by multiple strings in array
问题是过滤具有 2 个类别的项目不会返回任何内容。因此,通过过滤"mulder",应该返回类别为"mulder"和"scully"的项目
我有定义以下内容的数据:
$scope.allnodes = [
{ name: "This is node 1", category: ["mulder"] },
{ name: "Another title is this one", category: ["scully"] },
{ name: "This is Another title", category: ["mulder"] },
{ name: "And finally a fourth title", category: ["mulder","scully"] }
];
相关的应用程序js是:
$scope.categories = ['Mulder','Scully'];
$scope.filterByCategory = function (node) {
return $scope.filter[node.category] || noFilter($scope.filter);
};
function noFilter(filterObj) {
for (var key in filterObj) {
if (filterObj[key]) {
return false;
}
}
return true;
}
这是部分渲染:
<div ng-controller="myCtrl">
<b>Category:</b>
<div ng-repeat="cat in categories">
<b><input type="checkbox" ng-model="filter[cat]" />{{cat}}
</b>
</div>
<hr />
<div ng-repeat="node in filtered=(allnodes | filter:filterByCategory)">
{{node.title}}
<div ng-repeat="term in node.category">
<label class="label label-info">{{term}}</label>
</div>
</div>
<hr />
Number of results: {{filtered.length}}
</div>
http://jsfiddle.net/kevinhowbrook/h2bekshb/6/
问题是node.category是一个数组。
您的 filterByCategory 应如下所示:
$scope.filterByCategory = function(node) {
for (var key in $scope.filter) {
if ($scope.filter[key] && (!node.category || node.category.indexOf(key) < 0)) {
return false;
}
}
return true;
};
此函数检查当前项目的类别是否包含筛选器中的所有选定类别。
http://jsfiddle.net/cutm3p9c/1/
相关文章:
- 在 JavaScript 中使用正则表达式从字符串中过滤出百分比
- 从字符串 JavaScript 中过滤多个正则表达式
- 使用同位素插件,我可以按字符串名称过滤并使图像重叠吗?
- 过滤#标记后返回一个字符串数组
- 使用 jQuery/Javascript 过滤字符串
- Javascript:按字符串数组过滤对象数组
- 如何使用JavaScript(不是jQuery)从字符串中过滤前缀和修剪单词
- 如何在javaScript中过滤从特定字符开始到字符的字符串
- 从 jquery/javascript 中字符串的第一个字符过滤表数据
- 通过数组中的多个字符串进行角度过滤
- Javascript 通过匹配输入来过滤字符串
- 使用JavaScript过滤字符串属性上的JSON
- Javascript -如何过滤字符串?(有效字符必须在[A.]Z]或[a].Z]或[0.9])
- 过滤字符串的起始值
- 我可以调用什么来过滤字符串结果?
- ng-grid上的升序/降序过滤字符串
- Javascript正则表达式过滤字符串
- 使用Javascript过滤字符串中的正则表达式
- 过滤字符串末尾的空间,同时在相同位置保留特殊字符
- Highcharts过滤字符串x轴