1

AngularJS Plunker document.write('');

<div ng-init="users = [{name:'John', phone:'555-1276', secret:'shhh'},
                       {name:'Mary', phone:'800-BIG-MARY', secret:'psst'},
                       {name:'Mike', phone:'555-4321', secret:'shhh'},
                       {name:'Adam', phone:'555-5678', secret:'shhh'},
                       {name:'Julie', phone:'555-8765', secret:'shhh'}]"></div>
 <input ng-model="search.name"/>
<table>
    <tr ng-repeat="user in users | filter:{name: search.name, phone: search.name}">
        <td>{{user.name}}</td>
        <td>{{user.phone}}</td>
        <td>{{user.secret}}</td>
    </tr>
</table>

How can i specify more than one field in filter criteria? Want to create a general function (without custom filter function)

http://plnkr.co/edit/NCnTnKfqTVAKsHeK96NE?p=preview

Jobelle
  • 2,717
  • 1
  • 15
  • 26
  • Possible duplicate of [Filtering by Multiple Specific Model Properties in AngularJS (in OR relationship)](http://stackoverflow.com/questions/13216115/filtering-by-multiple-specific-model-properties-in-angularjs-in-or-relationship).. even your example is from one of the answers on this question.. – tanmay May 11 '17 at 10:26
  • @tanmay how can i pass params from view to this filter function? – Jobelle May 11 '17 at 10:54
  • @tanmay want to create a general function for that :). If we can pass the params as array to this function. then it will ok :) – Jobelle May 11 '17 at 10:59
  • @tanmay like – Jobelle May 11 '17 at 11:01
  • http://stackoverflow.com/a/16227326 explains it nicely. here's your plunker with such sample: http://plnkr.co/edit/dz3xzgvBuzuOqrfR4VEs?p=preview – tanmay May 11 '17 at 11:07
  • @tanmay But http://plnkr.co/edit/1aFTAWDJUjtGrVUOZ9UY?p=preview they have uded a normal function rather than filter – Jobelle May 11 '17 at 11:11
  • yeah but they are not passing any variables from the HTML too like you want – tanmay May 11 '17 at 11:12
  • @tanmay How can we use params there?/ thats my dbt :) – Jobelle May 11 '17 at 11:18
  • can't pass params to filter functions.. if you want to pass params, you need to create `app.filter('...')` AFAIK – tanmay May 11 '17 at 11:19

2 Answers2

0

Yo may create a custom filter like this:

 app.filter("cFilter",function(){
  return function(items,search){
    var filteredItems = [];
    search = search || '';
    search = search.toUpperCase();
    angular.forEach(items,function(i,n)
    {
      var cItem = items[n];
      if(cItem.name.toUpperCase().indexOf(search)>-1 ||cItem.phone.toUpperCase().indexOf(search)>-1)
        {
          filteredItems.push(items[n]);
        }
      else if (search==undefined)
        {
          filteredItems.push(cItem);
        }
    });

    return filteredItems;
  }
  }
);

and change the html this way:

<tr ng-repeat="user in users | cFilter:search.name ">
            <td>{{user.name}}</td>
            <td>{{user.phone}}</td>
            <td>{{user.secret}}</td>
        </tr>
Alberto
  • 13
  • 2
-2

You need to change your filter with this.

<tr ng-repeat="user in users | filter:search.name">