5

I have following filter in angularJS:

<div ng-repeat="node in data | filter:{parentID:12}"></div>

This works fine (I get only data, where parentID is 12).

In the next step, I want to get all data, where parentID is (for example) 12,13,25 or 30.

I tried following (its not working):

filter:{parentID:[12,13,25,30]}

Is there any way to build a filter as described?

Thank you very much!

Tream
  • 1,049
  • 2
  • 13
  • 29
  • possible duplicate of [How to filter multiple values (OR operation) in angularJS](http://stackoverflow.com/questions/15868248/how-to-filter-multiple-values-or-operation-in-angularjs) – Gruff Bunny Oct 28 '14 at 09:34

1 Answers1

6

A predicate function will suit your needs nicely! From the doc:

function(value, index): A predicate function can be used to write arbitrary
filters. The function is called for each element of array. The final result
is an array of those elements that the predicate returned true for.

For example:

<div ng-repeat="node in data | filter:checkParentID"></div>

And in your controller

$scope.checkParentID = function(value, index) {
  return value.parentID && [12,13,25,30].indexOf(value.parentID) !== -1;
}
morloch
  • 1,781
  • 1
  • 16
  • 23
  • @Tream Oops, I confused everything. Filter should look like filter:parentFilter. Please accept morloch's answer, because it's correct and mine is not. – dfsq Oct 28 '14 at 10:13
  • Not sure, what you did wrong - works for me. But okay, I accepted his answer. Thanks you and morloch for helping me! :) – Tream Oct 28 '14 at 10:20
  • How can I achieve this but on a particular attribute of the collection – Zakaria Belghiti Feb 04 '16 at 02:12
  • You mean an attribute of data? Just pass that to `ng-repeat`, e.g. `node in data.attr...` – morloch Feb 04 '16 at 04:23