逗号分隔对象上的多选过滤器

时间:2014-12-08 20:50:52

标签: javascript html json angularjs filter

使用angularjs我希望有一些复选框,在选中时,它们会根据任意数量的标签对数据应用过滤器。选择。属性tag包含逗号分隔值。每个值都将归入一个复选框。

复选框:Shoulder, Knee, Hand, Foot, Core Stability, Back

数据:{ "id": "2", "name": "Neutral Spine on Gym Ball - Single Leg and Arm Lift ", "perform_set": "3", "perform_rep": "20", "perform_rest": "30 sec", "tags": ["Shoulder","Core Stability","Back"] },

我有大约4000条记录,结构如下。我愿意以不同方式安排标签,这些标签尚未添加到数据中。理想情况下,我需要从一开始就认为这是正确的,因为每次练习都要花费很多时间。

非常感谢提前。

2 个答案:

答案 0 :(得分:2)

根据您在问题中对我的评论,应该是您想要的:

(忽略下面的代码段,因为我需要链接到plnkr)

http://plnkr.co/edit/Xg7tGAQvBy3C6PjNROSH?p=preview

enter image description here

$scope.ApplyFilter = function() {
   $scope.FilteredData = $scope.Data;
   for (var i = 0; i < $scope.ActiveFilters.length; i++) {
      $scope.FilteredData = $filter('filter')($scope.FilteredData, $scope.ActiveFilters[i], undefined);
   }
    };

答案 1 :(得分:0)

下面是一些伪代码,它代表了许多不同方法中的一种。

    <input type="checkbox" ng-model='filters.neck'>
    <input type="checkbox" ng-model='filters.leg'>

   <tr ng-repeat="data in yourdata | filter: filteredData(data)">

    $scope.filters = {"neck":false,"leg":false}
    $scope.currentFilters = [];
    $scope.$watch("filters",$scope.setFilters,true);

    $scope.setFilters = function() {
       // Something changed. Construct an arrow that containers the items to filter
       // e.g. $scope.currentFilters = ["neck"] because neck is checked
    };

    $scope.filtereddata = function (item) {
      var ret = false;
      if(item.currentFilters contains one of the items in item.tags ) {
         ret = true;
      } 
      return ret;
    };