We have answer of your question!

100% solved queries, no empty question

Question: AngularJS Ng-Repeat Search Filter, How can i limit what properties to search for?


0

Advertisement


Currently i have a ng-repeat that writes out a rather large object per row. However, i am only wanting the search filter to scan through 5 properties and not the 20 properties the object has. Is there a way i can tell the filter to only check specific properties and ignore the rest? I was thinking something below...

<div> <input ng-model="search" type="text"/> </div>
<div ng-repeat="item in items | filter:search:property1:property2">
   {{item.property1}}
</div>
Question author User1732364 | Source

Answer


1


Advertisement


Create a custom filter that allows you to have necessary parameters as you need.

app.filter('myFilter', function(){
  // Just add arguments to your HTML separated by :
  // And add them as parameters here, for example:
  // return function(dataArray, searchTerm, argumentTwo, argumentThree) {
  return function(dataArray, searchTerm) {
      // If no array is given, exit.
      if (!dataArray) {
          return;
      }
      // If no search term exists, return the array unfiltered.
      else if (!searchTerm) {
          return dataArray;
      }
      // Otherwise, continue.
      else {
           // Convert filter text to lower case.
           var term = searchTerm.toLowerCase();
           // Return the array and filter it by looking for any occurrences of the search term in each items id or name. 
           return dataArray.filter(function(item){
              var termInId = item.id.toLowerCase().indexOf(term) > -1;
              var termInName = item.name.toLowerCase().indexOf(term) > -1;
              return termInId || termInName;
           });
      } 
  }    
});

Then in HTML

<tr ng-repeat="item in data | myTableFilter:filterText:argumentTwo:argumentThree">

Inspired from filter-by-multiple-columns-with-ng-repeat

Answer author Sajeetharan

Advertisement


Tickanswer.com is providing the only single recommended solution of the question AngularJS Ng-Repeat Search Filter, How can i limit what properties to search for? under the categories i.e angularjs , . Our team of experts filter the best solution for you.

Related Search Queries:

You may also add your answer!