1

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>
user1732364
  • 925
  • 4
  • 28
  • 58

2 Answers2

3

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

Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

Not sure you can do directly . I think you will have to write a function in your controller to do that .

e.g.

    <div> <input ng-model="search" type="text"/> </div>
    <div ng-repeat="item in items | filter:searchSpecificPpts()">
       {{item.property1}}
    </div>

in your controller create a function like
   $scope.searchSpecificPpts(){
      //Here goes the logic to filter
      return filteredList;
   }
Bhabani Das
  • 380
  • 5
  • 13
  • The problem i had with this approach was i couldn't get the text the user was searching by to do any comparisons. – user1732364 Jan 11 '17 at 16:27
  • You are taking the user input the ng-model="search" So in the $scope.searchSpecificPpts() function use that as $scope.search .. you will have access to the property. – Bhabani Das Jan 11 '17 at 16:41