简体   繁体   中英

How to filter multiple values (OR operation) in angularJS (continued)

I can across this very useful angular filter. I wanted to alter it so it removes any items that aren't explicitly filtered for. Right now if any key has a property which isn't recognized it ignores they key entirely. Essentially I want items filtered out if a key's property isn't present. For instance if $scope.searchName doesn't match it should be removed by the filter.

I got the filter here. https://stackoverflow.com/a/21169596/2292822

<div ng-repeat="item in filtered = (items | filterMultiple:queryBy)>

Here is the relevant scope variables and filter

$scope.queryBy = {name:$scope.searchName, y1:$scope.selectedYear,y2:$scope.selectedYear,y3:$scope.selectedYear,y4:$scope.selectedYear};



myApp.filter('filterMultiple',['$filter',function ($filter) {return function (items, keyObj) {
var filterObj = {
    data:items,
    filteredData:[],
    applyFilter : function(obj,key){
        var fData = [];
        if (this.filteredData.length == 0)
            this.filteredData = this.data;
        if (obj){
            var fObj = {};
            if (!angular.isArray(obj)){
                fObj[key] = obj;
                fData = fData.concat($filter('filter')(this.filteredData,fObj));
            } else if (angular.isArray(obj)){
                if (obj.length > 0){
                    for (var i=0;i<obj.length;i++){
                        if (angular.isDefined(obj[i])){
                            fObj[key] = obj[i];
                            fData = fData.concat($filter('filter')(this.filteredData,fObj));    
                        }
                    }
                }
            }
            if (fData.length > 0){
                this.filteredData = fData;
            }
        }
    }
};
if (keyObj){
    angular.forEach(keyObj,function(obj,key){
        filterObj.applyFilter(obj,key);
    });
}
return filterObj.filteredData;}}]);

I made a few changes to the code, it works for me now, hopefully it works for you too!

myApp.filter('filterMultiple',['$filter',function ($filter) {return function (items, keyObj) {
        var x = false;
        var filterObj = {
            data:items,
            filteredData:[],
            applyFilter : function(obj,key){
                var fData = [];
                if (this.filteredData.length == 0 && x == false)
                    this.filteredData = this.data;
                if (obj){
                    var fObj = {};
                    if (!angular.isArray(obj)){
                        fObj[key] = obj;
                        fData = fData.concat($filter('filter')(this.filteredData,fObj));
                    } else if (angular.isArray(obj)){
                        if (obj.length > 0){
                            for (var i=0;i<obj.length;i++){
                                if (angular.isDefined(obj[i])){
                                    fObj[key] = obj[i];
                                    fData = fData.concat($filter('filter')(this.filteredData,fObj));    
                                }
                            }
                        }
                    }
                    if (fData.length > 0){
                        this.filteredData = fData;
                    }
                    if (fData.length == 0) {
                        if(obj!= "" && obj!= undefined)
                        {
                            this.filteredData = fData;
                            x = true;
                        }
                    }

                }
            }
        };
        if (keyObj){
            angular.forEach(keyObj,function(obj,key){
                filterObj.applyFilter(obj,key);
            });
        }
        return filterObj.filteredData;}}]);

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