AngularJS 在某些字段中使用 "OR" 条件进行过滤
AngularJS filter in certain fields with "OR" condition
我对 AngularJS 过滤器有疑问。
我不想 "filter" 使用一个输入搜索框,而是使用 "OR" 条件使用多个字段过滤列表。
这里是演示。
http://jsbin.com/wodewexetu/2/edit
我不想过滤只有 name
和 description
字段的列表。
如果我键入:"hello"、"world" 或 "angular" 它应该不会 return 任何结果。
如果我输入:"example",它应该 return 只有第一行和第三行。
如果我输入:"description" 它应该 return 只有第一行和第二行。等等
所以一般来说,我想用一个名字,多个,但列表中的某些字段 "OR" 条件进行过滤。
我能够满足 "AND" 条件,但这不是我需要的。
我也搜索了很多关于这个主题的内容,但不幸的是,这些代码和示例都不适合我。
请帮助并提前致谢。
我认为还不支持 OR 过滤器。我通过创建一个名为 $searchable
.
的元 属性 解决了这个问题
angular.forEach($scope.items, function (item){
Item.$searchable = [item.name, item.description].join (' ');
}
在html中:
<input type="text" ng-model="search.$searchable">
您可以创建自己的 custom filter,并应用您想要的任何功能。
在你的情况下,类似于:
app.filter('customFilter', function() {
return function(items, search) {
if (!search) {
return items;
}
var searchItems = new RegExp(search.split(' ').join("|"), "i");
return items.filter(function(item) {
return searchItems.test(item.name) || searchItems.test(item.description);
});
};
});
看到它工作 here。
我对 AngularJS 过滤器有疑问。
我不想 "filter" 使用一个输入搜索框,而是使用 "OR" 条件使用多个字段过滤列表。
这里是演示。
http://jsbin.com/wodewexetu/2/edit
我不想过滤只有 name
和 description
字段的列表。
如果我键入:"hello"、"world" 或 "angular" 它应该不会 return 任何结果。
如果我输入:"example",它应该 return 只有第一行和第三行。
如果我输入:"description" 它应该 return 只有第一行和第二行。等等
所以一般来说,我想用一个名字,多个,但列表中的某些字段 "OR" 条件进行过滤。
我能够满足 "AND" 条件,但这不是我需要的。
我也搜索了很多关于这个主题的内容,但不幸的是,这些代码和示例都不适合我。
请帮助并提前致谢。
我认为还不支持 OR 过滤器。我通过创建一个名为 $searchable
.
angular.forEach($scope.items, function (item){
Item.$searchable = [item.name, item.description].join (' ');
}
在html中:
<input type="text" ng-model="search.$searchable">
您可以创建自己的 custom filter,并应用您想要的任何功能。
在你的情况下,类似于:
app.filter('customFilter', function() {
return function(items, search) {
if (!search) {
return items;
}
var searchItems = new RegExp(search.split(' ').join("|"), "i");
return items.filter(function(item) {
return searchItems.test(item.name) || searchItems.test(item.description);
});
};
});
看到它工作 here。