【问题标题】:Angular checkbox filter leaves no data角度复选框过滤器不留下任何数据
【发布时间】:2015-06-29 05:43:57
【问题描述】:

我的复选框过滤器完全删除所有数据记录而不是过滤。 我用这篇文章作为指导: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;
    }

【问题讨论】:

  • 你能只保留重现问题的最少代码吗?你能设置一个jsfiddle吗?

标签: javascript angularjs filtering


【解决方案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 函数将返回两个数组的交集。

【讨论】:

  • 这非常有效。仍然习惯于这些古怪的 javascript 系统税。谢谢!
  • 如果您有任何问题,尽管问;)
  • 我试图了解整个功能。是不是对于每个项目,我们都会得到索引,即 n?然后获取匹配类别的item的索引?
  • filter 遍历给定数组并将每个数组项的引用作为参数n 传递给匿名函数。如果项目存在于另一个数组中,filter 中的匿名函数将返回真值(布尔值)。请参阅:developer.mozilla.org/en/docs/Web/JavaScript/Reference/…developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
  • 好的,我明白了。内置的东西可以正常工作,比如 .filter。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
相关资源
最近更新 更多