Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度复选框过滤器不留下任何数据_Javascript_Angularjs_Filtering - Fatal编程技术网

Javascript 角度复选框过滤器不留下任何数据

Javascript 角度复选框过滤器不留下任何数据,javascript,angularjs,filtering,Javascript,Angularjs,Filtering,我的复选框过滤器完全删除所有数据记录,而不是过滤。 我用这篇文章作为指导: 我可以获取并控制所选类别。我在筛选列表时遇到问题 我的数据如下: {"product_title":"COPD Track Package","product_code":"COPDPKG2014","id":"a1u40000000C182AAC","Id":"a1u40000000C182AAC","sort_order":"6","sort_code":"COPDPKG2014","category":["Post

我的复选框过滤器完全删除所有数据记录,而不是过滤。 我用这篇文章作为指导:

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

我的数据如下:

{"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;
    }

搜索过滤器
按产品名称:
按产品代码:
按演示文稿标题:
按发言者姓名:
研究生课程
继续教育
筛选器转储:{{productIncludes}
排序方式:
产品代码
产品名称
产品代码产品名称
{{item.id}{{c}
{{item.product_title}
$scope.productIncludes=[];
$scope.includeProduct=功能(类别){
var i=$.inArray(类别,$scope.productIncludes);
如果(i>-1){
$scope.productIncludes.splice(i,1);
}否则{
$scope.productIncludes.push(类别);
}
log($scope.items.length);
}
$scope.productFilter=函数(项){
如果($scope.productIncludes.length>0){
if($.inArray(items.cat,$scope.productIncludes)<0)返回;
}
退货项目;
}

基本上,这是数据表示的问题

您将类别存储为“
”类别“:[“研究生课程,继续教育”]
,您希望在这些类别中搜索,就像它是一个类似于“
”类别“:[“研究生课程”,“继续教育”]
的数组一样

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

$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;
}
$scope.productFilter=函数(项){
如果($scope.productIncludes.length>0){
var result=$scope.productIncludes.filter(函数(n){
退货项目.类别.索引(n)!=-1
});
如果(结果长度<1){
返回;
}
}
退货项目;
}
这是你的小提琴:

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

您能否只保留再现问题的最小代码?您可以设置一个JSFIDLE吗?这非常有效。仍然习惯于javascript中这些看起来古怪的systax。非常感谢。如果您有任何问题,请询问;)我试图理解整个功能。是不是每一项都有一个索引,就是n?然后获取具有匹配类别的项的索引?
filter
迭代给定数组,并将每个数组项的引用作为参数
n
传递给匿名函数。
filter
中的匿名函数返回另一个数组中存在项时的真实值(布尔值)。看:好的,我明白了。内置的东西,就像。过滤器。