我为我的ng表实现了一个自定义过滤器,它使用ngTagInput。Link[1]与我的代码类似,这个过滤器只对当前页面起作用。在ng-repeat中过滤所有结果的正确方法是什么?
过滤器代码片段:
.filter('filterByTags', function () {
return function (items, tags) {
var i = 0;
var filtered = []; // Put here only items that match
(items || []).forEach(function (item) { // Check each item
var matches = tags.some(function (tag) { // If there is some tag
i++;
return (item.name.indexOf(tag.name) > -1) // that is a substring
}); // we have a match
if (matches) { // If it matches
filtered.push(item); // put it into the `filtered` array
}
});
if(i == 0){
return items;
}
else{
return filtered;
}
};
})
[1]在AngularJS中过滤ngtagsinput
我还遇到了过滤器只能在当前页面上工作的问题。最初的问题不包括HTML,只是一个到类似代码的链接,所以我不确定我的解决方案是否适用于最初的问题,但它可能会帮助那些在分页和过滤方面苦苦挣扎的人。我的HTML看起来是这样的,在过滤之前有分页:
<tr ng-repeat="item in items | pages: myCtrl.currentPage : myCtrl.itemsPerPage | filter: searchCriteria">
我修改了HTML,将过滤放在分页之前,然后过滤器在所有页面上工作:
<tr ng-repeat="item in items | filter: searchCriteria | pages: myCtrl.currentPage : myCtrl.itemsPerPage">