2

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

2
  • what is the desired output Commented Apr 5, 2013 at 0:27
  • to be able to filter out elements based on the searchVoucher value. Say searchVoucher=4, then only one <li> is to be displayed in DOM Commented Apr 5, 2013 at 0:39

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what i needed. Thanks. Appreciate your prompt reply and elegant solution.
1

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.

1 Comment

Thanks a lot. Learned something new from your fiddle ;). However the solution given by Arun is what i needed. Thanks again. Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.