JavaScript - Filter JSON Data In AngularJS

In this tutorial we will create a Filter JSON Data In AngularJS. This code will filter the json data when the user enter a keyword in the form. The code itself use an AngularJS directives that has a several built-functionalities that can filter a data in the table by binding the textbox with ng-model and adding a filter parameter in the ng-repeat. This is a free source code you can modify it and use it on your working programs. We will be using AngularJS as 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 have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. Lastly, you will need to download the AngularJS here's the link https://angularjs.org/. Note: This code will only work if run in a local server.

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 - Filter JSON Data In AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                
  18.                 <div class="col-md-8">
  19.                         <div class="form-inline pull-right">
  20.                                 <input type="text" class="form-control" ng-model="filter" placeholder="Search here..."/>
  21.                         </div>
  22.                         <br /><br />
  23.                         <table class="table table-bordered">
  24.                                 <thead class="alert-info">
  25.                                         <tr>
  26.                                                 <th>Firstname</th>
  27.                                                 <th>Lastname</th>
  28.                                                 <th>Address</th>
  29.                                         </tr>
  30.                                 </thead>
  31.                                 <tbody>
  32.                                         <tr ng-repeat="member in members | filter: filter">
  33.                                                 <td>{{member.firstname}}</td>
  34.                                                 <td>{{member.lastname}}</td>
  35.                                                 <td>{{member.address}}</td>
  36.                                         </tr>
  37.                                 </tbody>
  38.                         </table>
  39.                 </div> 
  40.         </div>
  41. <script src="js/angular.js"></script>  
  42. <script src="js/script.js"></script>   
  43. </body>
  44. </html>

Creating the Main Function

This code contains the main function of the application. This code will filter the JSON data from a JSON file. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below. member.json [ {"firstname": "John", "lastname": "Smith", "address": "New York"}, {"firstname": "Claire", "lastname": "Temple", "address": "Racoon City"} ] script.js Note: Save this file inside the js directory as script.js to make the program work.
  1. var app = angular.module('myModule', [])
  2.                                         .controller('myController', function($scope, $http){
  3.                                                 $http.get('member.json').then(function(response){
  4.                                                         $scope.members = response.data;
  5.                                                 });
  6.                                                
  7.                                         });
There you have it we successfully created a Filter JSON Data In 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