Skip to main content

Sort HTML Table Using AngularJS

Screenshot
Body
In this tutorial we will create Sort HTML Table using AngularJS. The code will sort the table data when you click the header. This code will Feel free to use this code and modify as you learn from it. To learn more about this, just follow the steps below.

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 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">Sort HTML Table Using AngularJS</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-8">
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th ng-click="sortData('firstname')">Firstname<div ng-class="getSortClass('firstname')"></div></th>
  23. <th ng-click="sortData('lastname')">Lastname<div ng-class="getSortClass('lastname')"></div></th>
  24. <th ng-click="sortData('address')">Address<div ng-class="getSortClass('address')"></div></th>
  25. </tr>
  26. </thead>
  27. <tr ng-repeat="member in members | orderBy:sortColumn:reverseSort">
  28. <td>{{member.firstname}}</td>
  29. <td>{{member.lastname}}</td>
  30. <td>{{member.address}}</td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. </div>
  35. <div class="col-md-4">
  36. <form>
  37. <div class="form-group">
  38. <label>Firstname</label>
  39. <input type="text" class="form-control" ng-model="newMember.firstname"/>
  40. </div>
  41. <div class="form-group">
  42. <label>Lastname</label>
  43. <input type="text" class="form-control" ng-model="newMember.lastname"/>
  44. </div>
  45. <div class="form-group">
  46. <label>Address</label>
  47. <input type="text" class="form-control" ng-model="newMember.address"/>
  48. </div>
  49.  
  50. <center><button type="button" class="btn btn-primary" ng-click = "saveMember()"><span class = "glyphicon glyphicon-save"></span> Save</button></center>
  51. </form>
  52. </div>
  53. </div>
  54. <script src="js/angular.js"></script>
  55. <script src="js/script.js"></script>
  56. </body>
  57. </html>

Creating the Main Function

This code contains the main function of the application. This code will sort the data when user click the table header. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js folder as script.js
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. $scope.members = [
  4. {firstname: "John", lastname: "Wick", address: "New York"},
  5. {firstname: "Steve", lastname: "Roger", address: "Japan"},
  6. ];
  7.  
  8. $scope.newMember = {};
  9.  
  10. $scope.saveMember = function(){
  11. $scope.members.push($scope.newMember);
  12. $scope.newMember = {};
  13. };
  14.  
  15. $scope.reverseSort = false;
  16.  
  17. $scope.sortData = function(column){
  18. $scope.reverseSort = ($scope.sortColumn == column) ? !$scope.reverseSort : false;
  19. $scope.sortColumn = column;
  20.  
  21. }
  22.  
  23. $scope.getSortClass = function(column){
  24. if($scope.sortColumn == column){
  25. return $scope.reverseSort ? 'down-arrow' : 'up-arrow';
  26. }
  27. return '';
  28. }
  29. });
There you have it we successfully created a Sort HTML Table 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