如何根据Angular js中的两个自定义过滤器过滤数据

时间:2022-08-25 09:52:59

I have two custom filters, I want to filter my data using both these custom filters. But i faced the problem, If I used one by one then they work good, But when I try to use both filters at the same time then no output. My code is as follow:

我有两个自定义过滤器,我想使用这两个自定义过滤器过滤我的数据。但我遇到了问题,如果我一个接一个地使用,那么它们工作正常,但是当我尝试同时使用两个过滤器时却没有输出。我的代码如下:

<script>
     var myApp = angular.module('myApp', []);
     myApp
.filter('selectedTags', function() {
    return function(postdata, tags) {
        return postdata.filter(function(task) {

            for (var i in task.tarn_list) {
                if (tags.indexOf(task.tarn_list[i]) != -1) {
                    return true;
                }
            }
            return false;

        });
    };
})
.filter('selectedDep', function() {
    return function(postdata, tags) {
        return postdata.filter(function(task) {

            for (var i in task.deployment) {
                if (tags.indexOf(task.deployment[i]) != -1) {
                    return true;
                }
            }
            return false;

        });
    };
})
.controller('PostList', ['$scope', '$http', function($scope, $http) {
           var jsonFile='../../json.php';
           $http.get(jsonFile).success(function(data) {
            $scope.postdata = data;
           });
           $scope.useMakes=[]
           $scope.checkBoxModel={
                    search:[],
                    ddsearch:[]
                };
           $scope.totalFeatures=features;
           $scope.deployment=all_deployment;
        }]);
</script>

My div is as follow on which I want to apply filter:

我的div如下所示我想应用过滤器:

<div ng-repeat="record in postdata | selectedDep:checkBoxModel.ddsearch | selectedTags:checkBoxModel.search" >

1 个解决方案

#1


3  

Not having seen the actual dataset, this here should float the boat I reckon - given the properties you've exposed in your question and the nature of the loops;

没有看到实际的数据集,这里应该漂浮我估计的船 - 考虑到你在问题中暴露的属性和循环的性质;

https://jsfiddle.net/op7m14m1/1/

https://jsfiddle.net/op7m14m1/1/


Instead of for in loops, I've opted for nested filters (which is in essence what you're doing).

我没有选择in循环,而是选择了嵌套过滤器(这实际上就是你正在做的事情)。

var predicate = [];

dataset.filter(function (a) {
  var inner = a.inner.filter(function (b) {
    return predicate.indexOf(b) > -1;
  });

  return inner.length > 0; 
});

Looking at the two filters you have, you could break it down into a single function with a bound (or passed in) parameter dictating which property to use as a matcher for the filter(s).

查看您拥有的两个过滤器,您可以将其分解为一个带有绑定(或传入)参数的函数,该参数指示哪个属性用作过滤器的匹配器。

Something like this;

像这样的东西;

function generic () {
  return function (prop, dataset, predicate) {
    return dataset.filter(function (element) {

      var innards = element[prop].filter(function (iEl) {
        return predicate.indexOf(iEl) > -1;
      });

      return innards.length > 0;
    });
  }
}

And then to use it you could do the following;

然后使用它你可以做到以下几点;

 module.filter('genericFilter', generic);
 module.filter('selectedDep',   generic.bind(null, 'deployment');
 module.filter('selectedTags',  generic.bind(null, 'tarn_list');

 // $filter('genericFilter')('deployment', [], ['a']);
 // $filter('selectedDep')([], ['b']);
 // $filter('selectedTags')([], ['c']);

This setup allows for a single function, that you can reuse to your heart's content - simply pass in the property you would like to do a deep filter of, or bind it preemptively.

此设置允许单个功能,您可以重复使用您的内容 - 只需传入您想要进行深度过滤的属性,或者预先绑定它。

#1


3  

Not having seen the actual dataset, this here should float the boat I reckon - given the properties you've exposed in your question and the nature of the loops;

没有看到实际的数据集,这里应该漂浮我估计的船 - 考虑到你在问题中暴露的属性和循环的性质;

https://jsfiddle.net/op7m14m1/1/

https://jsfiddle.net/op7m14m1/1/


Instead of for in loops, I've opted for nested filters (which is in essence what you're doing).

我没有选择in循环,而是选择了嵌套过滤器(这实际上就是你正在做的事情)。

var predicate = [];

dataset.filter(function (a) {
  var inner = a.inner.filter(function (b) {
    return predicate.indexOf(b) > -1;
  });

  return inner.length > 0; 
});

Looking at the two filters you have, you could break it down into a single function with a bound (or passed in) parameter dictating which property to use as a matcher for the filter(s).

查看您拥有的两个过滤器,您可以将其分解为一个带有绑定(或传入)参数的函数,该参数指示哪个属性用作过滤器的匹配器。

Something like this;

像这样的东西;

function generic () {
  return function (prop, dataset, predicate) {
    return dataset.filter(function (element) {

      var innards = element[prop].filter(function (iEl) {
        return predicate.indexOf(iEl) > -1;
      });

      return innards.length > 0;
    });
  }
}

And then to use it you could do the following;

然后使用它你可以做到以下几点;

 module.filter('genericFilter', generic);
 module.filter('selectedDep',   generic.bind(null, 'deployment');
 module.filter('selectedTags',  generic.bind(null, 'tarn_list');

 // $filter('genericFilter')('deployment', [], ['a']);
 // $filter('selectedDep')([], ['b']);
 // $filter('selectedTags')([], ['c']);

This setup allows for a single function, that you can reuse to your heart's content - simply pass in the property you would like to do a deep filter of, or bind it preemptively.

此设置允许单个功能,您可以重复使用您的内容 - 只需传入您想要进行深度过滤的属性,或者预先绑定它。