Skip to main content

JavaScript - Search Filter And Sort Using AngularJS


In this tutorial we will create a Search Filter And Sort Using AngularJS. This code can filter and sort an array of data within the html table. The code use angular directives to sort and filter array element by binding the textbox in ng-model then a adding a filter function in the ng-repeat with the same value as the model and also by providing an string argument to getSortClass() in the table th. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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 it as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charsert="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. <link rel="stylesheet" type="text/css" href="css/style.css">
  7. </head>
  8. <body ng-controller="myController">
  9. <nav class="navbar navbar-default">
  10. <div class="container-fluid">
  11. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12. </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">JavaScript - Search Filter And Sort Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <div class="form-inline">
  21. <input type="text" class="form-control" ng-model="search" placeholder="Search here..."/>
  22. </div>
  23. <br />
  24. <table class="table table-bordered">
  25. <thead class="alert-info">
  26. <tr>
  27. <th ng-click="sortData('firstname')">Firstname<div ng-class="getSortClass('firstname')"></div></th>
  28. <th ng-click="sortData('lastname')">Lastname<div ng-class="getSortClass('lastname')"></div></th>
  29. <th ng-click="sortData('gender')">Gender<div ng-class="getSortClass('gender')"></div></th>
  30. </tr>
  31. </thead>
  32. <tr ng-repeat="member in members | orderBy:sortColumn:reverseSort | filter: search">
  33. <td>{{member.firstname}}</td>
  34. <td>{{member.lastname}}</td>
  35. <td>{{member.gender}}</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 Script

This code contains the main function of the application. This code will sort and filter the table data when the user manipulate the inputs. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. var members = [
  4. {firstname: "John", lastname: "Smith", gender: "Male"},
  5. {firstname: "Claire", lastname: "Temple", gender: "Female"},
  6. ];
  7.  
  8. $scope.members = members;
  9. $scope.reverseSort = false;
  10.  
  11. $scope.sortData = function(column){
  12. $scope.reverseSort = ($scope.sortColumn == column) ? !$scope.reverseSort : false;
  13. $scope.sortColumn = column;
  14.  
  15. }
  16.  
  17. $scope.getSortClass = function(column){
  18. if($scope.sortColumn == column){
  19. return $scope.reverseSort ? 'down-arrow' : 'up-arrow';
  20. }
  21. return '';
  22. }
  23. });

There you have it we successfully created a Search Filter And Sort 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!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.