Angular复选框过滤器不会留下数据

时间:2015-04-21 20:45:47

标签: javascript angularjs filtering

我的复选框过滤器完全删除了所有数据记录而不是过滤。 我使用这篇文章作为指导:http://jsfiddle.net/65Pyj/

我可以获得并控制所选类别。我在筛选列表时遇到问题。

我的数据是这样的:

{"product_title":"COPD Track Package","product_code":"COPDPKG2014","id":"a1u40000000C182AAC","Id":"a1u40000000C182AAC","sort_order":"6","sort_code":"COPDPKG2014","category":["Postgraduate Course, Continuing Education"]}

这是角度:

<div class="container">
<div class="ng-scope" ng-app="products">
<div class="ng-scope" ng-controller="ShoppingCartCtrl">   
<div class="row">
<div class="col-sm-7"><h3 class="colored-title">Search Filter</h3>
<table class="table table-striped table-bordered">
<tbody>

<tr>
<td>By Product Title:</td>
<td><input ng-model="search.product_title" type="text"/></td>
</tr>
<tr>
<td>By Product Code:</td>
<td><input ng-model="search.product_code" type="text"/></td>
</tr>
<tr>
<td>By Presentation Title:</td>
<td><input ng-model="search.presentations" type="text"/></td>
</tr>
<tr>
<td>By Speaker Name:</td>
<td><input ng-model="search.speakers" type="text"/></td>
</tr>

<tr>
    <td><input type="checkbox" ng-click="includeProduct('Postgraduate Course')"/>Postgraduate Course</td>
    <td><input type="checkbox" ng-click="includeProduct('Continuing Education')"/>Continuing Education</td>
</tr>
    <tr><td>Filter dump: {{productIncludes}}</td></tr>
</tbody>
</table></div>

</div>

<div>Sort by:<select ng-model="sortExpression">
<option value="sort_code">Product Code</option>
<option value="product_title">Product Title</option>
</select></div>

<table class="table table-bordered table-striped">
<thead>
<tr class="warning"><th>Product Code</th><th>Product Title</th></tr>
</thead>
<tbody>
<tr ng-repeat="item in items | orderBy:mySortFunction | filter:search |filter:productFilter">
<td valign="top">
{{item.id}}<div ng-repeat="c in item.cat" >{{c}}</div>
</td>
<td>
{{item.product_title}}
</tr>
</tbody>
</table>
</div>




$scope.productIncludes = [];

        $scope.includeProduct = function(category){
            var i = $.inArray(category, $scope.productIncludes);
            if(i > -1){
                $scope.productIncludes.splice(i,1);
            }else{
                $scope.productIncludes.push(category);   
            }

            console.log($scope.items.length);
        }


         $scope.productFilter = function (items) {
        if ($scope.productIncludes.length > 0) {
            if ($.inArray(items.cat, $scope.productIncludes) < 0) return;
        }

        return items;
    }

1 个答案:

答案 0 :(得分:1)

基本上,您的数据表示存在问题。

您将类别存储为"category":["Postgraduate Course, Continuing Education"],并且您希望在这些类别中搜索,就好像它是"category":["Postgraduate Course", "Continuing Education"]这样的数组。

我建议您像第二种情况一样表示您的数据,然后您可以像这样迭代它:

$scope.productFilter = function (items) {
  if ($scope.productIncludes.length > 0) {
    var result = $scope.productIncludes.filter(function(n) {
      return items.category.indexOf(n) != -1
    });
    if(result.length < 1) {
      return;
    }
  }
  return items;
}

这是你的小提琴:http://jsfiddle.net/65Pyj/133/

简短说明: filter函数与indexOf函数组合将返回两个数组的交集。