Shekhar
Shekhar

Reputation: 807

How can we use filter in ng-repeat when iterating on an object in AngularJs

Search field binded to a model

<input type="text" ng-model="searchVoucher" />

An object iterated in ng-repeat

<li ng-repeat="(k,f) in {'r':4,'e':5,'t':6,'y':7,'c':8} | filter:searchVoucher">{{f}}</li>

How can i filter based on the object's key or val or val can also be an object having attributes.

Please help

Upvotes: 2

Views: 1579

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388416

Try

<li ng-repeat="(k,f) in {'r':4,'e':5,'t':6,'y':7,'c':8} | searchfilter:searchVoucher">{{f}}</li>

Filter

app.filter('searchfilter', function() {
  return function(input, term) {
    var regex = new RegExp(term || '', 'i');
    var obj = {};
    angular.forEach(input, function(v, i){
      if(regex.test(v + '')){
        obj[i]=v;
      }
    });
    return obj;
  };
});

Demo: Plunker

Upvotes: 2

Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6954

Please have a look at this http://jsfiddle.net/fFLUH/. Here the same filter searchVoucher is configured to filter the input based on key or value, based on the filterParam. The keyword this that is passed from li reffers to the controller tst. This tst controller has the model for the textboxes.

Inside the filter, im accessing the filterParam, and using that to filter the input json.

Upvotes: 1

Related Questions