Apply dynamic filters using Tags

后端 未结 1 1662
逝去的感伤
逝去的感伤 2021-01-16 10:56

I have a list of elements which is displayed in a table using ng-repeat. I want to apply dynamic filters which are added using Tags(ng-tags-input). This tag input generates

1条回答
  •  猫巷女王i
    2021-01-16 11:34

    If you want to include items that have a property-value that matches at least one of the tags, you can define your custom filter like this:

    app.filter('filterByTags', function () {
        return function (items, tags) {
            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
                    return (item.data1.indexOf(tag.text) > -1) || // that is a substring
                           (item.data2.indexOf(tag.text) > -1);   // of any property's value
                });                                               // we have a match
                if (matches) {           // If it matches
                    filtered.push(item); // put it into the `filtered` array
                }
            });
            return filtered; // Return the array with items that match any tag
        };
    });
    

    And the use it like this:

    
    

    See, also, this short demo.

    0 讨论(0)
提交回复
热议问题