简体   繁体   中英

Custom filter in AngularJS after adding pagination

After implementing the pagination to my ng-repeat listing ( Update pagination in AngularJS after filtering )

<li ng-repeat="data in filtered = (list | filter:search) ... >

I now have a problem with my custom filter

<li ng-repeat="data in filtered = (list | filter:search) | customFilter:search ... >

I need this filter to search by multiple languages (select two or more languages). If I you replace data in filtered = (list | filter:search) with data in list , you will see it's working. But I need filtered for my pagination.

jsFiddle: http://jsfiddle.net/StinsonMaster/SuEX6/4/ (based on the fiddle from the previous thread )

I would suggest, instead of using an expression in your ng-repeat directive, set the ng-repeat equal to a method on the list's constroller that returns the subset or values you're looking for. For example:

// In your controller

        $scope.filteredList = function() {
           return $filter('filter')($scope.list,{'language':search.language});
        }

// And in your ng-repeat 

        ng-repeat="(key,val) in filteredList()"

Hope this helps!

I think I misunderstood your original question.

I rewrote your custom filter. It's not exactly objected oriented, but with a little touching up it could be much more extensible. Basically you just needed something that was more inclusive than the base angular filter.

app.filter("customFilter", function() {
    return function(input, search) {
        if(search && search != undefined) {
            var _toRet = new Array();
            for(var i in search) {
                for(var k in input) {
                    if(search.indexOf(input[k].language) != -1 && _toRet.indexOf(input[k]) == -1) {
                        _toRet.push(input[k]);
                    }
                }
            }
            return _toRet;
        } else {
            return input;
        }
    };
});

Also please note the changes to your ngRepeat syntax.

ng-repeat="data in filtered = list | filter:search.name | customFilter:search.language | startFrom:(currentPage-1)*entryLimit | limitTo:entryLimit"

http://jsfiddle.net/doublekid/5Bs3h/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM