javascript - Filter by key in (key, val) pair using ng-model -


i ahave following data:

[   {     "rub": {       "item1": 979,       "item2": 32,       "item3": 845     },     "shop": "shop1",   },   {     "rub": {       "item232": 84,       "item213": 348     },     "shop": "shop2"   } ] 

i try filter in table key using ng-model. isn't filtering @ all.

<table class="table ng-cloak" ng-repeat="rub in rubs | filter:isactive" ng-if='isactive'>   <input type="text" class="form-control" placeholder="Товар" ng-model="rub.rub[key]">     <thead>       <tr>         <th>#</th>         <th>Товар</th>         <th>Число</th>       </tr>     </thead>     <tbody>       <tr ng-repeat='(key, val) in rub.rub'>         <td>{{ $index }}</td>         <td>{{ key }}</td>         <td>{{ val }}</td>       </tr>                       </tbody>   </table> 

my controller:

currycontrollers.controller('curryrubricsctrl', ['$scope', '$routeparams', '$http', '$route',   function($scope, $routeparams, $http, $route) {     $scope.cityid = $routeparams.cityid;     $http.get('cities.json').success(function(data) {       $scope.cities = data;     $http.get('json/shop_data.json').success(function(data2) {       $scope.rubs = data2;        $scope.isactive = function(item) {     return item.shop === $scope.cityid;   };   });   }); 

i've tried add $scope.searchrub = '' controller , put form in html template.

  <form>     <div class="form-group">       <div class="input-group">         <div class="input-group-addon"><i class="fa fa-search"></i></div>          <input type="text" class="form-control" placeholder="Поиск" ng-model="searchrub">        </div>           </div>   </form> 

added 'searchrub' filter here : <td> {{ key | filter:searchrub }} </td> didn't either.

you want search box model independent value can use filter, rather trying model key of object using.

<input type="text" class="form-control" placeholder="Товар" ng-model="search”> 

there number of ways use value filter, easiest ng-show:

<tr ng-repeat='(key, val) in rub.rub' ng-show="search ? search===key : true"> 

here’s plunk. i’ve hardcoded cityid avoid using routeparams demo.

https://plnkr.co/edit/si8habnkbbjgmx0fedid?p=preview

type "item1" search box.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -