JavaScript - Search Filter And Sort Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 150 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">JavaScript - Search Filter And Sort Using AngularJS</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-2"></div>
  20. <div class="col-md-8">
  21. <div class="form-inline">
  22. <input type="text" class="form-control" ng-model="search" placeholder="Search here..."/>
  23. </div>
  24. <br />
  25. <table class="table table-bordered">
  26. <thead class="alert-info">
  27. <tr>
  28. <th ng-click="sortData('firstname')">Firstname<div ng-class="getSortClass('firstname')"></div></th>
  29. <th ng-click="sortData('lastname')">Lastname<div ng-class="getSortClass('lastname')"></div></th>
  30. <th ng-click="sortData('gender')">Gender<div ng-class="getSortClass('gender')"></div></th>
  31. </tr>
  32. </thead>
  33. <tr ng-repeat="member in members | orderBy:sortColumn:reverseSort | filter: search">
  34. <td>{{member.firstname}}</td>
  35. <td>{{member.lastname}}</td>
  36. <td>{{member.gender}}</td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </div>
  41. </div>
  42. <script src="js/angular.js"></script>
  43. <script src="js/script.js"></script>
  44. </body>
  45. </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!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.