<checkbox> Angular leaves no data

My filter checkbox completely removes all data records, not filtering. I used this post as a guide: http://jsfiddle.net/65Pyj/

I can get the console of the selected category too. I'm having trouble filtering the list.

My data is like this:

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

      

Here is angular:

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

      

+3


source to share


1 answer


This is mainly a problem with your data views.

You have categories saved as "category":["Postgraduate Course, Continuing Education"]

, and you want to search in those categories as if it were an array like "category":["Postgraduate Course", "Continuing Education"]

.

I recommend that you present your data as the second case, and then you can iterate over it like so:



$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;
}

      

And here is your fiddle: http://jsfiddle.net/65Pyj/133/

Short explanation: A function filter

combined with a function indexOf

returns the intersection of two arrays.

+1


source







All Articles