How to ignore property in angular filter

I am trying to ignore a property called title in my angular. I have a dataset as shown below:

const data = [
    {
        title: 'Title 1'
        groups: [
            {...},
            {...},
            {...}
        ]
    },
    {
        title: 'Title 2'
        groups: [
            {...},
            {...},
            {...}
        ]
    },
    {
        title: 'Title 3'
        groups: [
            {...},
            {...},
            {...}
        ]
    }
];

      

And I use ng-repeat with a filter to iterate over objects and another loop to iterate over groups:

<input ng-model="search">
<div ng-repeat="item in data | filter:search">
    <h1>{{item.title}}</h1>
    <ul>
        <li ng-repeat="group in item.group | filter:search">
            <span>{{group.something}}</span>
        </li>
    </ul>
</div>

      

Works fine, but now I would like to ignore the title in the search. I tried several things like: filter:search:item.title

(in the first ng retry) or remove the first filter:search

one but all attempts failed. What am I missing? Do I need custom search or something like that?

Thank.

+3


source to share


3 answers


You can specify the properties you want to filter and leave the title:

<li ng-repeat="group in item.groups | filter: { something: search }">

      



The above code will only filter based on the something property.

More answers and explanations here: AngularJS filters only certain objects

+1


source


If you type and don't filter the title property, just remove the first filter. This way, when you put in a match li isnt, it will be hidden, but their h1 will stay in the same place.



0


source


You should create a custom filter where you can specify which property should be excluded ( ignored ) for reasons:

angular.module('app', []).controller('MyController', ['$scope', function($scope) {
    $scope.data = [
      {name:"Tom", title:'London'},
      {name:"Max", title:'Moscow'},
      {name:"Henry", title:'NY'},
      {name:"Paul", title:'NY'},
      {name:"Sam", title:'Paris'}
      ];
}]).filter('myfilter',function(){
  return function(input, search, ignore){
    if(!search)
      return input;
      
    var result = [];
    
    for(var item of input)
      for(var prop in item)
        if(prop != ignore && item[prop].indexOf(search) != -1)
        {
          result.push(item) ;
          break;
        }
      
    return result;
  }
});
      

<script src="//code.angularjs.org/snapshot/angular.min.js"></script>

<body ng-app="app">
  <div ng-controller="MyController">
    search: <input type='text' ng-model='search' ng-init='search="a"'/>
    ignore: <input type='text' ng-model='ignore' ng-init='ignore="title"'/>
 <ul>
   <li ng-repeat='item in data | myfilter: search: ignore'>
     {{item.name}} {{item.title}}
   </li>
 </ul>
</div>
</body>
      

Run codeHide result


0


source







All Articles