angularjs - Angular 1.7 Filtering Table based off select box -
http://jsfiddle.net/adukg/13415/
i have object called 'orders' contains list of foods, filterable type select box below. select box contains filter food category of orders. want able filter out table using angular using select box.
$scope.orders = { "order1": {food_name: "apple", type: 1}, "order2": {food_name: "banana", type: 1}, "order3": {food_name: "carrot", type: 2}, "order4": {food_name: "cereal", type: 3}, "order5": {food_name: "wheat", type: 3} } $scope.foodcategories = [{id:0,name:"all"}, {id:1,name:"fruit"},{id:2,name:"vegatable"}, {id:3,name:"grains"}];
html:
<div ng-app = "myapp" ng-controller="foodcntrl"> select food type: <select class="form-control" ng-model="foodcategories" ng-options="type type.name type in foodcategories"> </select> <table class="table"> <thead> <tr> <th>food name</th> </tr> </thead> <tbody> <tr ng-repeat="f in orders | filter:foodcategories.id"> <td>{{f.food_name}}</td> </tr> </tbody> </table> </div>
i getting angular console error: error: filter:notarray not array when using filter:foodcategories on table ng-repeat.
how can fix this?
just add filter property "type" in orders array, , "foodcategories" make id "all" category empty string {id:"",name:"all"}.
<table class="table"> <thead> <tr> <th>food name</th> </tr> </thead> <tbody> <tr ng-repeat="f in orders | filter:{type: doortypeselect.id}"> <td>{{f.food_name}}</td> </tr>
here working plunkr: http://plnkr.co/edit/4v0i1aigrovnousp7eom?p=preview
Comments
Post a Comment