Understanding filters

By now, we know how to transfer data from a backend or user into an HTML page. This section will focus on how to show and manipulate data using AngularJS. The AngularJS framework has another useful component: filters. Filters return a subset of the item. In AngularJS, a filter is a global function that can be implemented in the view using a pipe symbol (|) and the parameter separated by a colon (:). AngularJS filters are used to format data in the view, for example when we want to display a number up to two decimals even if the user enters the value with more than two decimals. The advantage of using the AngularJS filter is that you do not need to register a function on the scope. Filters are available for each and every DOM template, and a filter can also be parameterized. The following table shows a list of significant built-in filters in AngularJS:

Filter name

Description

orderBy

This is used to sort the array, for example orderBy:Name.

limitTo

This is used to limit the number of items to be displayed, for example limitTo:5.

lowercase and uppercase

This is used to transform the text to lowercase or uppercase.

filter

This is used to search within an array.

currency

This is used to add the currency symbol to a given value when there is no currency symbol, for example, {{amount|currency}}, {{amount|currency:"Rs."}}.

date

This is used to format the date in various formats.

json

This is used to convert the JavaScript object into JSON.

number

This is used to format the number value, such as to round a decimal number, make it negative, or fix decimal places.

The following code example shows how to use the AngularJS filter for searching an array:

var app = angular.module('myApp', []);

app.controller("ctrlFilterExample", function ($scope) {
    $scope.cars = [
      { name: "Honda", model: 2014 },
      { name: "Toyota", model: 2015 },
      { name: "Mercedes", model: 2013 },
      { name: "Ford", model: 2016 },
      { name: "BMW", model: 2014 },
      { name: "Porsche", model: 2015 }
    ];
});

We can render the HTML element as follows:

<div ng-app="myApp" ng-controller="ctrlFilterExample">

<input ng-model="carName" type="text" placeholder="Filter by" autofocus><br />

<ul ng-repeat="car in cars | filter:carName | orderBy: 'name' ">

<li>{{car.name}}</li>

</ul>
</div>

We can use multiple filters at a time. In the AngularJS framework, chaining filters is a good way of implementing more than one filter. AngularJS filters can be applied to strings, objects, or function parameters. In the preceding example, we first applied a filter on the string parameter, but gave an array of the $scope.cars object. We used the ng-repeat directive to display the array and apply two filters using the pipe symbol (|). With the first filter, we searched inside the $scope.car array when the user typed inside the textbox. Then, we used the ng-model directive in the <input> element and assigned the variable name as carName. The model variable was then passed to the filter:carName filter. Second, we filtered the array by name using the orderBy filter, orderBy: 'name'.

The custom filter

The AngularJS framework allows us to create custom filters. In the following code example, we create a filter that will show a check mark if the car model year is the current year, otherwise the filter will show a cross:

var app = angular.module('myApp', []); 

app.controller("ctrlFilterExample", function ($scope) { 
$scope.cars = [ 
{ name: "Honda", model: 2014 }, 
{ name: "Toyota", model: 2015 }, 
{ name: "Mercedes", model: 2013 }, 
{ name: "Ford", model: 2016 }, 
{ name: "BMW", model: 2014 }, 
{ name: "Porsche", model: 2015 } 
]; 
}); 
app.filter("markCurrentYear", function () { 
return function (input) { 
var currentYear = new Date().getFullYear(); 

if (input == currentYear) { 
return input+' ' + 'u2713'; 
}
else 
{ 
return input +' ' + 'u2718'; 
} 
}; 
}); 

We can render the HTML elements as follows:

<div ng-app="myApp" ng-controller="ctrlFilterExample">

<ul ng-repeat="car in cars | orderBy: 'name'">
<li>{{car.name}} - {{car.model|markCurrentYear}}</li>
</ul>

</div>

The output of the preceding code is as follows:

The custom filter

As you can see in HTML in the preceding code, we called this the {{car.model | markCurrentYear}} filter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset