The filter functionality of AngularJS is often used inside the view when you need to filter a set of items in a collection. I had overlooked the usefulness this might have as a tool inside the controller as well. It can be very useful in “querying” information.
When I say “using filter” I am referring to the filter named “filter” not just the $filter service.
Suppose you have the following:
// sample collection of objects var myObjects = [ { name: "Object1", shape: "circle", color: "red" }, { name: "Object2", shape: "square", color: "orange" }, { name: "Object3", shape: "triangle", color: "yellow" }, { name: "Object4", shape: "circle", color: "green" }, { name: "Object5", shape: "sphere", color: "blue" }, { name: "Object6", shape: "hexagon", color: "indigo" }, { name: "Object7", shape: "square", color: "violet" }, { name: "Object8", shape: "triangle", color: "red" } ];
You can easily get an array of just the objects matching a specific criteria:
// return an array with only red objects var myRedObjects = $filter('filter')(myObjects, { color: "red" });
This also works if you need to match on multiple properties:
var myRedObjects = $filter('filter')(myObjects, { shape: "circle", color: "red" });
The filter always returns an array whether there is many, one or zero matching objects:
// return an array with only green objects (in this case - an array of one var myGreenObject = $filter('filter')(myObjects, { color: "green" }); // When you don't get anything back (returns an empty array) var niceTry = $filter('filter')(myObjects, { color: "white" });
If you are feeling lucky, you can even try to grab the first element for immediate use. (Although it would probably be good form to actually check to see if you got a result back first.)
// If you are feeling really lucky that you are going to find one // and want to just grab the first one var luckyGrab = $filter('filter')(myObjects, { color: "green" })[0];
- Limiting Concurrent Subscriptions With Rx - October 18, 2017
- Authorization/Permissions with Thruway - November 5, 2015
- Integrating Symfony Authentication for ThruwayBundle - June 22, 2015
it gives matching results.
For example if you filter which are having “acc” values, it also gives those containing “accepted” in o/p .
Can you post for exact match filter?
Be aware that if you need to filter for an exact match, i.e. on an id property, then make sure you pass ‘true’ as the third argument to the filter function.
How to do something that those rows should be returned shape==’square’ OR color==”orange’
And finally having result with 2 rows where:-
{ name: “Object2”, shape: “square”, color: “orange” },
{ name: “Object7”, shape: “square”, color: “violet” },
Hey, do you think it’s better to filter in the controller or in the view?
The “normal” way to use the filter is inside the view.
Whether one way or the other is better really depends on what you are trying to do. If your filter is needed to help your view present information to the user, then it makes sense to keep that in the view. Same with a search box that is filtering a local data source.
This post just highlights the fact that you can use the filter in the controller as well if there is a situation where you want to find an object or objects in a collection using controller code.
for anyone still wondering, filtering in the view hinders performance, check out this article:
https://toddmotto.com/use-controller-filters-to-prevent-digest-performance-issues/
Great man! Thanks!
How to use OR condition?
Hi,
Thanks for the tutorial.
But how can you filter rows that have color of ‘red’ OR ‘Indigo’.
I couldn’t find it anywhere!
Hi Shilan,
To do more complex logic, you can use a function as a selector:
$filter(‘filter’)(myObjects, function (o) {
return o.color == “red” || o.color == “indigo”;
});
Hope that helps.
how to return only exact match (case sensitive words) in this?
Hi,
Thanks for the tutorial.
my selected array is :- $scope.selection = [‘red’, ‘green’];
var myObjects = $filter(‘filter’)(myObjects, { color: “red” });
Here how i can filter row that have color of ‘red’ and ‘green’
Thanks!
Thanks a lot for share your knowledge. its very useful for me.
Really Appreciate the article…it is very helpful!!…Sabyasachi from India
you are the best, works great, once you add $filter to the controller
This made my day and saved to me a lot of time and dirty code … Thank you!