Angular "multiple" 值过滤器

Angular "multiple" value filter

我需要一些有关 angular 过滤器的帮助。我需要通过一个字段中的多个值过滤数组。

数组如下所示:

$scope.items = [{
  "item": "one",
  "tags": "2,5"
}, {
  "item": "two",
  "tags": "3,4,6,7"
}, {
  "item": "three",
  "tags": "1,3,5"
}];

我想按以逗号分隔的标签搜索项目。

示例:在搜索字段中,用户输入的标签用逗号分隔(或 select 通过复选框分隔)和 select 标签 1 和 3。如何列出具有这些标签的所有项目?在此示例中,第二项和第三项。

笨蛋

https://plnkr.co/edit/6SidABYsjtrjtH3xqusA?p=preview

  <table>
     <tr>
        <td align="right">Search :</td>
        <td><input ng-model="query" /></td>
     </tr>
     <tr>
        <td align="right">Search By :</td>
        <td>
           <select ng-model="query">
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              <option value="4">4</option>
           </select>
        </td>
     </tr>
  </table>
  <hr>
  <div>
     <table>
        <tr ng-repeat="item in items | filter:query">
           <td>{{item.item}}</td>
        </tr>
     </table>



  var employeeApp = angular.module("EmployeeApp",[]);
  employeeApp.controller("empCtrl",function($scope){

    $scope.items = [{
      "item": "one",
      "tags": "2,5"
    }, {
      "item": "two",
      "tags": "3,4,6,7"
    }, {
      "item": "three",
      "tags": "1,3,5"
    }];
    $scope.orderProp="";                
  });

这是您需要使用复选框的答案。

angular.module('app', [])
  .controller('Controller', function($scope) {
    $scope.items = [{
      "name": "one",
      "tags": "2,5"
    }, {
      "name": "two",
      "tags": "3,4,6,7"
    }, {
      "name": "three",
      "tags": "1,3,5"
    }];

    $scope.items_dup = $scope.items
    // checkbox selection
    $scope.selection = [];
    $scope.toggleSelection = function toggleSelection(filter) {
      var idx = $scope.selection.indexOf(filter);
      if (idx > -1) {
        $scope.selection.splice(idx, 1);
      } else {
        $scope.selection.push(filter);
      }
    };

    // filter list
    $scope.filter = function() {
      filterTag($scope.selection,$scope.items);
      function filterTag(selected,items){
        var out = [];
        angular.forEach(items, function(value, key) {
          angular.forEach(selected, function(inner_value, key) {
            if(value.tags.split(',').indexOf(inner_value.toString()) > -1)
            {
              if(out.indexOf(value) == -1)
              {
                out.push(value)
              }
            }
          })
        })
        if(out.length > 0)
        {
          $scope.items_dup = out;
        }
        else
        {
          $scope.items_dup = $scope.items;   
        }
      }
      
    };
  })
<!DOCTYPE html>
<html>

<head>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
</head>
<body ng-app="app">
  <div ng-controller="Controller">
    <h1>Tag filter!</h1>
    <li ng-repeat="item in items_dup">
      {{item.name}}
    </li>
    <hr>
    <p>Select filter</p>
    <label>
      <input type="checkbox" value="1" ng-click="toggleSelection(1)"> 1
    </label>
    <br>
    <label>
      <input type="checkbox" value="2" ng-click="toggleSelection(2)"> 2
    </label><br>
    <label>
      <input type="checkbox" value="3" ng-click="toggleSelection(3)"> 3
    </label>
    <br><br>
    <button ng-click="filter()">Filter list</button>
  </div>
</body>

</html>

请运行代码段并检查。

Here is the plunker