Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS custom filter function

Inside my controller, I would like to filter an array of objects. Each of these objects is a map which can contain strings as well as lists

I tried using $filter('filter')(array, function) format but I do not know how to access the individual elements of the array inside my function. Here is a snippet to show what I want.

$filter('filter')(array, function() {   return criteriaMatch(item, criteria); }); 

And then in the criteriaMatch(), I will check if each of the individual property matches

var criteriaMatch = function(item, criteria) {   // go thro each individual property in the item and criteria   // and check if they are equal } 

I have to do all these in the controller and compile a list of lists and set them in the scope. So I do need to access the $filter('filter') this way only. All the examples I found in the net so far have static criteria searches inside the function, they don't pass an criteria object and test against each item in the array.

like image 616
user2368436 Avatar asked May 10 '13 02:05

user2368436


People also ask

What is custom filter in AngularJS?

Introduction to AngularJS Custom Filter. In AngularJS filters are used to modify or update the data before rendering the data on view or UI. Filters are clubbed in expression or directives using pipe (|) symbol.

What is the correct way to apply multiple filter in AngularJS?

Using filters in view templates Filters can be applied to the result of another filter. This is called "chaining" and uses the following syntax: {{ expression | filter1 | filter2 | ... }} E.g. the markup {{ 1234 | number:2 }} formats the number 1234 with 2 decimal points using the number filter.

What is the role of a filter in AngularJS?

AngularJS Filters allow us to format the data to display on UI without changing original format. Filters can be used with an expression or directives using pipe | sign. Angular includes various filters to format data of different data types.

What is a custom filter?

Custom filters allow you to define matching logic that cannot be accomplished using the system-provided message filters. For example, you might create a custom filter that hashes a particular message element and then examines the value to determine whether the filter should return true or false.


2 Answers

You can use it like this: http://plnkr.co/edit/vtNjEgmpItqxX5fdwtPi?p=preview

Like you found, filter accepts predicate function which accepts item by item from the array. So, you just have to create an predicate function based on the given criteria.

In this example, criteriaMatch is a function which returns a predicate function which matches the given criteria.

template:

<div ng-repeat="item in items | filter:criteriaMatch(criteria)">   {{ item }} </div> 

scope:

$scope.criteriaMatch = function( criteria ) {   return function( item ) {     return item.name === criteria.name;   }; }; 
like image 80
Tosh Avatar answered Sep 19 '22 15:09

Tosh


Here's an example of how you'd use filter within your AngularJS JavaScript (rather than in an HTML element).

In this example, we have an array of Country records, each containing a name and a 3-character ISO code.

We want to write a function which will search through this list for a record which matches a specific 3-character code.

Here's how we'd do it without using filter:

$scope.FindCountryByCode = function (CountryCode) {     //  Search through an array of Country records for one containing a particular 3-character country-code.     //  Returns either a record, or NULL, if the country couldn't be found.     for (var i = 0; i < $scope.CountryList.length; i++) {         if ($scope.CountryList[i].IsoAlpha3 == CountryCode) {             return $scope.CountryList[i];         };     };     return null; }; 

Yup, nothing wrong with that.

But here's how the same function would look, using filter:

$scope.FindCountryByCode = function (CountryCode) {     //  Search through an array of Country records for one containing a particular 3-character country-code.     //  Returns either a record, or NULL, if the country couldn't be found.      var matches = $scope.CountryList.filter(function (el) { return el.IsoAlpha3 == CountryCode; })      //  If 'filter' didn't find any matching records, its result will be an array of 0 records.     if (matches.length == 0)         return null;      //  Otherwise, it should've found just one matching record     return matches[0]; }; 

Much neater.

Remember that filter returns an array as a result (a list of matching records), so in this example, we'll either want to return 1 record, or NULL.

Hope this helps.

like image 27
Mike Gledhill Avatar answered Sep 19 '22 15:09

Mike Gledhill