You can learn more in my previous blogs,
In this blog we will see,
- How to filter data in AngularJS.
- Inbuilt filters
- Sort data filters
To apply a filter in AngularJS a pipe character ( | ) is used.
E.g.

Inbuilt filters in AngularJS
| Filter Name | Description |
| Lowercase | Converts all characters to lowercase. |
| Uppercase | Converts all characters to uppercase. |
| Number | Includes coma separators in a number. |
| Date | Converts date to a requested format (explained below). |
| Currency | $ is a default currency. Custom currencies can be added. |
Date filter
| Format | Result |
| yyyy | 4 digit year e.g. 1998 |
| yy | 2 digit year e.g. 1998 becomes 98 |
| MMMM | January-December |
| MMM | Jan-Dec |
| MM | 01-12 |
| M | 1-12 |
| dd | 01-31 |
| d | 1-31 |
AngularJS filter example
- Following is the simple html code without adding any filter to display the data in table format.
- <!DOCTYPEhtml>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script src="Scripts/angular.js">
- </script>
- <script src="Scripts/JavaScript6to10.js">
- </script>
- <link href="StyleSheet2.css" rel="stylesheet" />
- </head>
- <body ng-app="myModule" style="font-family:Arial">
- <div ng-controller="myController">
- <br/>
- <br/>
- <table>
- <thead>
- <tr>
- <th>Name</th>
- <th>Gender</th>
- <th>DateOfBirth</th>
- <th>Salary</th>
- <th>Salary</th>
- </tr>
- </thead>
- <tbody>
- <trng-repeat="employee in employees">
- <td>{{employee.Name}}</td>
- <td>{{employee.gender}}</td>
- <td>{{employee.dateOfBirth}}</td>
- <td>{{employee.Salary}}</td>
- <td>{{employee.Salary}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- <hr/>
- </body>
- </html>
- JavaScript Code,
- /// <reference path="angular.js" />
- varmyApp = angular
- .module("myModule", [])
- .controller("myController", function($scope)
- {
- var employees = [
- {
- Name: "John",
- gender: "male",
- Salary: 55000,
- dateOfBirth: new Date("May 23, 1950")
- },
- {
- Name: "Rekha",
- gender: "female",
- Salary: 45000,
- dateOfBirth: new Date("May 23, 1951")
- },
- {
- Name: "Mary",
- gender: "female",
- Salary: 35000,
- dateOfBirth: new Date("May 23, 1952")
- },
- {
- Name: "Smith",
- gender: "male",
- Salary: 25000,
- dateOfBirth: new Date("May 23, 1953")
- },
- ];
- $scope.employees = employees;
- });
- Following is the code for StyleSheet.
Output

- Following is the code fo the html page after adding filters.
- Use the same JavaScript Code.
- Filters are added for Name (uppercase, date, number, currency)
Output with filters- <!DOCTYPEhtml>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script src="Scripts/angular.js">
- </script>
- <script src="Scripts/JavaScript6to10.js">
- </script>
- <linkhref="StyleSheet2.css" rel="stylesheet" />
- </head>
- <body ng-app="myModule" style="font-family:Arial">
- <div ng-controller="myController">
- <br/>
- <br/>
- <table>
- <thead>
- <tr>
- <th>Name</th>
- <th>Gender</th>
- <th>DateOfBirth</th>
- <th>Salary</th>
- <th>Salary</th>
- </tr>
- </thead>
- <tbody>
- <trng-repeat="employee in employees">
- <td>{{employee.Name|uppercase}}</td>
- <td>{{employee.gender}}</td>
- <td>{{employee.dateOfBirth|date:"dd/MM/yyyy"}}</td>
- <td>{{employee.Salary|number:2}}</td>
- <td>{{employee.Salary|currency:"£"}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- <hr/>
- <hr/>
- <br/>
- </body>
- </html>
Name (uppercase) DateOfBirth(date)
Salary (number) Salary (currency)

Sort Data Filter
In AngularJS there are filters which are used to sort the data in ascending or descending order.

- To sort in ascending order, set reverse to false.
- To sort in descending order, set reverse to true.
- You can also use + and - to sort in ascending and descending order respectively.
- Following is the html code. By selecting the column through the dropdown list OrderBy filter is executed.
- Use the Same Stylesheet.
- <!DOCTYPEhtml>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <script src="Scripts/angular.js">
- </script>
- <scriptsrc="Scripts/JavaScript6to10.js">
- </script>
- <lin khref="StyleSheet2.css" rel="stylesheet" />
- </head>
- <body ng-app="myModule" style="font-family:Arial">
- <div ng-controller="myController">
- Sort By:
- <selectng-model="sortColumn">
- <option value="Salary">Salary</option>
- <option value="dateOfBirth">Date of Birth</option>
- <option value="gender">gender</option>
- </select>
- <br/>
- <br/>
- <table>
- <thead>
- <tr>
- <th>Name</th>
- <th>Gender</th>
- <th>DateOfBirth</th>
- <th>Salary</th>
- </tr>
- </thead>
- <tbody>
- <trng-repeat="employee in employees | orderBy:sortColumn">
- <td>{{employee.firstName}}{{employee.lastName}}</td>
- <td>{{employee.gender}}</td>
- <td>{{employee.dateOfBirth|date:"dd/MM/yyyy"}}</td>
- <td>{{employee.Salary}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- <hr/>
- <br/>
- </body>
- </html>
- Following is the JavaScript code. sortColumn property is initialized to salary so that data will be sorted by Salary for the first time.
Output with Salary column. Try it with another column also,- /// <reference path="angular.js" />
- varmyApp = angular
- .module("myModule", [])
- .controller("myController", function($scope)
- {
- var employees = [
- {
- firstName: "abc",
- lastName: "xyz",
- gender: "female",
- Salary: 55000,
- dateOfBirth: new Date("May 23, 1950")
- },
- {
- firstName: "def",
- lastName: "uvw",
- gender: "Male",
- Salary: 45000,
- dateOfBirth: new Date("May 23, 1951")
- },
- {
- firstName: "ghi",
- lastName: "rst",
- gender: "Male",
- Salary: 35000,
- dateOfBirth: new Date("May 23, 1952")
- },
- {
- firstName: "jkl",
- lastName: "pqr",
- gender: "female",
- Salary: 25000,
- dateOfBirth: new Date("May 23, 1953")
- },
- ];
- $scope.employees = employees;
- $scope.sortColumn = 'Salary';
- });


kalu singh raoPosted Jul 1, 2016, 1:48 AM
Nice...