0

The leftValue array contains JSON information such as { name: 'John', gender: 'male' } I am trying to filter information by both name and gender in a search bar. For now, the filter only filtered by name. How can I filter by both name and gender?

scope.leftValue = $filter('filter')( leftValue, {
                    'name': text
                    })

What I have tried

scope.leftValue = $filter('filter')( leftValue, {
                    'name': text,
                    'gender': text,
                    })

4 Answers 4

1

I think your getting a little mixed up. You can specify an object map like your second example. This is what the docs state about it when you do.

Object: A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1". A special property name $ can be used (as in {$:"text"}) to accept a match against any property of the object or its nested object properties. That's equivalent to the simple substring match with a string as described above. The predicate can be negated by prefixing the string with !. For example {name: "!M"} predicate will return an array of items which have property name not containing "M".

The important thing to take away here is the second sentence and the and. Meaning in order for a match the string, in your case text, has to match ALL properties specified in your map.

Searching for just male wont match if the name is only John for example. But searching for ma would return the following record:

{
   name: 'mark',
   gender: 'male'
}

Just for FYI you can also search by object map through the view but it has the same limitations.

That being said it is possible to use the $ wildcard giving it a comma separated list of properties. This will do an or match over any of the properties.

{
   $: 'name,gender'
}

The catch here is all properties will have the same value checked against them.

Here's a fiddle showing them in action.

The other answers sum up quite well the alternatives, just felt they were lacking in explaining what was happening and the reasons behind it.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi the link is not a fiddle link
I tried ''$': text, and it will check for all properties. I also tried '$: "name,gender"': text but it doesnt seems to work.
@zainu I'm not at my computer but when I am ill update the fiddle link.
0

You can use chain of filters.

 var result1 = $filter('filter')( leftValue, {'name': text })
  var result2 = $filter('filter')( result1 , {'gender': text })

1 Comment

Hi thanks for replying. But can chain of filters check for 'name' OR 'gender'?
0

i appreciate you said in the controller, but just to give you options you can have it all done in your HTML.

{{yourJSONLinkedToScope | filter: name | filter: gender}}

where name and gender are ng-models from the input boxes.

Comments

0

I think better we do it in html.

<tr ng-repeat="player in players | filter:{id: player_id, name:player_name} | filter:ageFilter">

$scope.ageFilter = function (player) {
    return (player.age > $scope.min_age && player.age < $scope.max_age);
}

You can check the way they do in this link:
AngularJS multiple filter with custom filter function

2 Comments

Please post the useful parts of links in the body of your answer. If the link goes down your answer will be of no use. I realize its a link to another SO question but if that question was ever deleted. Plus its just a good habit to get into.
Thanks for suggestion.

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.