I am trying to implement custom filter in angular js. The idea is that user can add some tags and each time filter is invokes.
Filter is a plain javascript object, basically it looks like this:
var filter = {color:'blue', length: 15};
Items are array of objects.
app.filter('filterByTags', function () {
return function (items, filter) {
debugger;
if (!isEmpty(filter)) {
var filtered = items;
for (var prop in filter) {
if (filter.hasOwnProperty(prop)) {
for (var i = 0; i < filtered.length; i++) {
if (filtered[i][prop] !== filter[prop]) {
filtered.splice(filtered.indexOf(filtered[i]), 1);
}
}
}
}
alert('Return filtered items');
return filtered;
} else {
alert('Original items ');
return items;
}
};
});
So if works fine when i add tag but when i remove this tag i should get initial items list, but i get already filtered list.
So for instance originally i have a list of 5 items and when i click some tag I should get 4 items. But when I remove this tag i should get 5 items again, but i still get 4 items.
I think it is because in this line of code:
return function (items, filter) {
when filter invokes second time, items are already filtered and returned list before. Any ideas how can i fix it?
Copyright Notice:Content Author:「user3127896」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31810636/angular-custom-filter-issue