JavaScript - Custom Filter Using AngularJS

In this tutorial we will create a Custom Filter Using AngularJS. AngularJS is a JavaScript-based open-source front-end web application framework . It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you will need to download & install the AngularJS here's the link https://angularjs.org/. And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body ng-controller="myController">
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">JavaScript - Custom Filter Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <table class="table table-bordered">
  18.                         <thead class="alert-info">
  19.                                 <tr>
  20.                                         <th>Firstname</th>
  21.                                         <th>Lastname</th>
  22.                                         <th>Civil Status</th>
  23.                                         <th>Address</th>
  24.                                 </tr>
  25.                         </thead>
  26.                         <tbody style="background-color:#fff;">
  27.                                 <tr ng-repeat="member in members | orderBy: 'lastname'">
  28.                                         <td>{{member.firstname}}</td>
  29.                                         <td>{{member.lastname}}</td>
  30.                                         <td>{{member.civil_status | civil_status}}</td>
  31.                                         <td>{{member.address}}</td>
  32.                                 </tr>
  33.                         </tbody>
  34.                 </table>
  35.         </div>
  36. <script src="js/angular.js"></script>  
  37. <script src="js/script.js"></script>   
  38. <script src="js/custom_filter.js"></script>    
  39. </body>
  40. </html>

Creating the Script

This code contains the main function of the application. This code will filter a portion of data into a new thing by getting the target id. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below. script.js
  1. var app = angular.module("myModule", [])
  2.                                         .controller("myController", function($scope){
  3.                                                 var members = [
  4.                                                         {firstname: "John", lastname: "Smith", civil_status: 1, address: "USA"},
  5.                                                         {firstname: "Claire", lastname: "Jacks", civil_status: 2, address: "Tokyo"},
  6.                                                         {firstname: "Venice", lastname: "Hudson", civil_status: 2, address: "Germany"},
  7.                                                         {firstname: "Luke", lastname: "Cage", civil_status: 2, address: "France"}
  8.                                                 ];
  9.                                                
  10.                                                 $scope.members = members;
  11.                                                
  12.                                         });
custome_filter.js
  1. app.filter("civil_status", function(){
  2.         return function(civil_status){
  3.                 switch(civil_status){
  4.                         case 1:
  5.                                 return "Single";
  6.                         case 2:
  7.                                 return "Married";
  8.                 }
  9.         }
  10. })     
There you have it we successfully created a Custom Filter Using AngularJS. I hope that this very simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment