【问题标题】:angular filter using an array of more than one value使用多个值的数组的角度过滤器
【发布时间】:2016-09-20 10:44:08
【问题描述】:

我有一个复选框列表:

 <input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value1'" ng-false-value="''"> value1<br>
 <input type="checkbox" ng-model="myFilter.filterField" ng-true-value="'value2'" ng-false-value="''"> value2<br>

.... …… 我想像这样过滤选中的复选框字段:

x in X | filter: { 'filterField': ['value1','value2']}

任何解决方案可以推入我的数组 filterField 并过滤多个值?谢谢!

【问题讨论】:

  • 您是否考虑过创建自己的过滤器?
  • 我需要更多 filterFields 并且可能这个 filterFields 也将是多个值的数组,我该怎么做??
  • 嗯...不是那么多xd也许我不明白,我是一个角度无知的人
  • 我举个例子

标签: angularjs


【解决方案1】:

来自AngularJS : Custom filters and ng-repeat

检查多个字段

// Filter, where element would be your x
$scope.filterFn = function(element)
{ 
    if(element.field === 'value1' || element.anotherField === 'value2')
    {
        return true; // this will be listed in the results
    }
    return false; // otherwise it won't be within the results
};

然后应用它
x in X | filter:filterFn

【讨论】:

    【解决方案2】:

    另一种方法是使用一个自定义过滤器并向其添加参数。这个小提琴展示了如何使用具有多个参数的过滤器: http://jsfiddle.net/halirgb/Lvc0u55v/

    这样

    myApp.filter('customFilter', function() { // Name of filter
    return function(first, second,third) {
    // write filter code here
      return second;// shows the last filter parameter
    };});
    

    并像这样使用过滤器:

      <div ng-repeat="x in X">  {{x|customFilter:myFilter.filterField}} </div>
    

    【讨论】:

      【解决方案3】:

      在这种情况下最好使用自定义过滤器

      $scope.filterField = function(x) {
          return x.filterField == 'value1' || x.filterField == 'value2' || x.anotherField == 'value';
      };
      
      
      x in X | filter: filterField
      

      【讨论】:

      • 但如果我想要更多过滤字段,我需要为每个字段创建一个函数???
      • 你可以像这样使用 x.filterField == 'value1' || x.field1 == '值' || x.field2 == 'value1' @Charly
      猜你喜欢
      • 2021-05-03
      • 2016-06-29
      • 1970-01-01
      • 2021-04-01
      • 2021-09-05
      • 1970-01-01
      • 2014-12-16
      • 2017-09-11
      • 1970-01-01
      相关资源
      最近更新 更多