0

How can I filter on ng-repeat with multiple filters on OR?

<div ng-repeat="account in accounts | filter: searchText | filter: functionFilter" >
...
</div>

I want that searchText filter with all fields and it do that, with functionFilter I want check other things and it do that, but I want these two filters working with logial OR but now they are working with AND.

As suggested, I tried

<div ng-repeat="account in accounts | filter: functionFilter || {value: searchText}" >
...
</div>

but it seems that works only functionFilter and also

<div ng-repeat="account in accounts | filter: functionFilter ? '' : searchText" >
...
</div>

simply show all.

8
  • 1
    Try with: filter: searchText || functionFilter Commented Jul 10, 2017 at 15:25
  • @PrerakSola, like that works only searchText Commented Jul 10, 2017 at 15:28
  • What is functionFilter? Is it a custom filter? Commented Jul 10, 2017 at 15:29
  • Is just a function that return true or false under certain condition Commented Jul 10, 2017 at 15:30
  • i think you can try it by writing single filter and do the both filter operation in single filter Commented Jul 10, 2017 at 15:36

2 Answers 2

0

It's very simple. Make a common function for this. Here it is searchText

Inside HTML...

<div ng-repeat="account in accounts | filter: searchText >
...
</div>

Inside Controlller...

I made the dummy example. You have to manage condition according to you.

$scope.searchText = function(item) {
    if (!$scope.query || (item.brand.toLowerCase().indexOf($scope.query) != -1) || (item.model.toLowerCase().indexOf($scope.query.toLowerCase()) != -1) ){
        return true;
    }
    return false;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, as I commented above, this is my first option, but I want to know if there is a way to concatenate more filters with logic OR
0

As suggested, you can use || operator between 2 filters.

I have create a sample fiddle to test the scenario.

Notice that

  • Initially the filter is by default applied by filterFunction and searchText both.
  • The moment you change the seachText value to "Harry" it shows only that result.
  • If the input box is cleared, than only filterFunction filter is applied.

3 Comments

If I change filerText it not apply filterFunction so it is not a logical OR, it is something like: apply filterFunction if filterText is empty but I need to apply filterFunction OR filterText independent on each status
It would be good if you can provide an example with plunkr or fiddle and demonstrate what exactly is required.
i updated your jsfiddle.net/uwb2s9tb/3 as you can see, I want to filter this, for example, with value "t" I need to maintain item 101 because in his name has a "t" and also item 102 because functionFilter return true and also his name has "t", instead I have only the second element (only the function will be applied) your example works only because the function filter like the normal filter, value "101" is checked on every field by default filter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.