Introduction
In this blog, we will learn how to use Angular JS Filters.
The following filters are available in Angular js:
- Currency: Format a number to a currency format.
- Date: Format a date to a specified format.
- Filter: Select a subset of items from an array.
- Json: Format an object to a JSON string.
- limitTo: Limits an array/string, into a specified number of elements/characters.
- Lowercase: Format a string to lower case.
- Number: Format a number to a string.
- orderBy: Orders an array by an expression.
- Uppercase: Format a string to upper case.
Step 1
Create an empty project in the Visual Studio of your choice.
Step 2
Add scripts and styles in head section.
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
- <script src="Employee.js"></script>
Step-3
Add web form for each filter right click project add new item choose web form or you can do all filters on one page -- it’s up to you.
Step-4
Create employee script and give the name Employee.js
- var app = angular
- .module("myModule", [])
- .controller("myController", function ($scope) {
- var employees = [
- { Name: "Airi Satou", Position: "Accountant", Office: "Tokyo", Salary: "162700", Start_Date:new Date("November 28, 2008") },
- { Name: "Bruno Nash", Position: "Software Engineer", Office: "London", Salary: "163500", Start_Date:new Date("May 03, 20113") },
- { Name: "Cedric Kelly", Position: "Senior Javascript Developer", Office: "Edinburgh", Salary: "106450", Start_Date: new Date("August 13, 2015") },
- { Name: "Dai Rios", Position: "Integration Specialist", Office: "Edinburgh", Salary: "162700", Start_Date: new Date("September 23, 2015") },
- { Name: "Finn Camacho", Position: "Pre-Sales Support", Office: "London", Salary: "163500", Start_Date: new Date("December 31, 2016") },
- { Name: "Garrett Winters", Position: "Sales Assistant", Office: "Edinburgh", Salary: "86000", Start_Date: "March 20, 2014" },
- { Name: "Hermione Butler", Position: "Customer Support", Office: "London", Salary: "132000", Start_Date: "Feb 1, 2017" },
- { Name: "Jackson Bradshaw", Position: "Support Engineer", Office: "Edinburgh", Salary: "206850", Start_Date: "Jan 10, 2010" },
- { Name: "Lael Greer", Position: "Software Engineer", Office: "London", Salary: "145600", Start_Date: "April 30, 2014" },
- { Name: "Martena Mccray", Position: "Team Leader", Office: "Edinburgh", Salary: "433060", Start_Date: "May 28, 2013" },
- { Name: "Olivia Liang", Position: "Developer", Office: "London", Salary: "145600", Start_Date: "July 11, 2015" },
- { Name: "Paul Byrd", Position: "Chief Financial Officer (CFO)", Office: "New York", Salary: "725000", Start_Date: "October 18, 2016" }
- ];
- $scope.employees = employees;
- });
Step 5
Design HTML web page:
- <body ng-controller="myController">
- <form id="form1" runat="server">
- <div class="container py-3">
- <h4 class="text-center text-uppercase">How to use filters in Angular js</h4>
- <table id="employee" class="table table-bordered">
- <thead class="bg-info text-white text-upparcase">
- <tr>
- <th>Name
- </th>
- <th>Position
- </th>
- <th>Office
- </th>
- <th>Salary
- </th>
- <th>Start Date
- </th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="employee in employees">
- <td>{{employee.Name}}</td>
- <td>{{employee.Position}}</td>
- <td>{{employee.Office}}</td>
- <td>{{employee.Salary}}</td>
- <td>{{employee.Start_Date}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </form>
- </body>
Currency filter
{{employee.Salary|currency}}
Date filter
{{employee.Start_Date|date:"dd/MM/yyyy"}}
- <body ng-controller="myController">
- <form id="form1" runat="server">
- <div class="container py-3">
- <h4 class="text-center">How to use <u>filter</u> in Angular js</h4>
- <label>Search: <input type="text" placeholder="search employees" class="form-control" ng-model="searchText"/></label>
- <table class="table table-bordered">
- <thead class="bg-info text-white text-upparcase">
- <tr>
- <th>Name
- </th>
- <th>Position
- </th>
- <th>Office
- </th>
- <th>Salary
- </th>
- <th>Joining Date
- </th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="employee in employees|filter:searchText">
- <td>{{employee.Name}}</td>
- <td>{{employee.Position}}</td>
- <td>{{employee.Office}}</td>
- <td>{{employee.Salary}}</td>
- <td>{{employee.Start_Date|date:"dd/MM/yyyy"}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </form>
- </body>
Json Filter
- <body ng-controller="myController">
- <form id="form1" runat="server">
- <div class="container py-3">
- <h4 class="text-center">How to use <u>json</u> filter in Angular js</h4>
- <pre>{{employees | json}}</pre>
- </div>
- </form>
- </body>
limitTo Filter
- <body ng-controller="myController">
- <form id="form1" runat="server">
- <div class="container py-3">
- <h4 class="text-center">How to use <u> limitTo</u> filter in Angular js</h4>
- <label>Rows to display : <input type="number" step="1" ng-model="rowCount" max="5" min="0" class="form-control" /></label>
-
- <table class="table table-bordered">
- <thead class="bg-info text-white text-upparcase">
- <tr>
- <th>Name
- </th>
- <th>Position
- </th>
- <th>Office
- </th>
- <th>Salary
- </th>
- <th>Joining Date
- </th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="employee in employees| limitTo:rowCount">
- <td>{{employee.Name}}</td>
- <td>{{employee.Position}}</td>
- <td>{{employee.Office}}</td>
- <td>{{employee.Salary}}</td>
- <td>{{employee.Start_Date|date:"dd/MM/yyyy"}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </form>
- </body
Lowercase
{{employee.Office|lowercase}}
Number
{{employee.Salary|number}}
Uppercase
{{employee.Position|uppercase}}
- <body ng-controller="myController">
- <form id="form1" runat="server">
- <div class="container py-3">
- <h4 class="text-center">How to use <u>orderBy</u> filter in Angular js</h4>
- <label>Sort By :</label>
- <select ng-model="sortColumn" class="custom-select col-md-3">
- <option value="Name">Name ASC</option>
- <option value="+Start_Date">Joining Date ASC</option>
- <option value="+Position">Position ASC</option>
- <option value="+Office">Office ASC</option>
- <option value="-Salary">Salary DESC</option>
- </select>
- <br />
- <br />
- <table class="table table-bordered">
- <thead class="bg-info text-white text-upparcase">
- <tr>
- <th>Name
- </th>
- <th>Position
- </th>
- <th>Office
- </th>
- <th>Salary
- </th>
- <th>Joining Date
- </th>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat="employee in employees|orderBy:sortColumn">
- <td>{{employee.Name}}</td>
- <td>{{employee.Position}}</td>
- <td>{{employee.Office}}</td>
- <td>{{employee.Salary}}</td>
- <td>{{employee.Start_Date|date:"dd/MM/yyyy"}}</td>
- </tr>
- </tbody>
- </table>
- </div>
- </form>
- </body>
Conclusion
In this blog, we have discussed all nine Angular js filters with a single Angular controller.
Step by step I have given example for all the filters. I hope you have understood all the filters.