Skip to main content

AngularJS - Sorting Table Header Source Code


In this tutorial we will create a Sorting Table Header using AngularJS. This code can sort an table row data in the HTML table when the table header is clicked. The code use angular directives to sort array data by adding orderBy in the ng-repeat and binding the table header with ng-model as the orderBy value. 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 has been used for the layout of the application 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">AngularJS - Sorting Table Header</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="sortHeader('firstname')">Firstname<div ng-class="sortClass('firstname')"></div></th>
  23. <th ng-click="sortHeader('lastname')">Lastname<div ng-class="sortClass('lastname')"></div></th>
  24. <th ng-click="sortHeader('gender')">Gender<div ng-class="sortClass('gender')"></div></th>
  25. </tr>
  26. </thead>
  27. <tr ng-repeat="member in members | orderBy:sortColumn:sortReverse | filter: search">
  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>
  36. <script src="js/angular.js"></script>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the Script

This code contains the main function of the application. This code will sorted the data from the table header. 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", address: "New York"},
  5. {firstname: "Claire", lastname: "Temple", address: "Racoon City"},
  6. ];
  7.  
  8. $scope.members = members;
  9. $scope.sortReverse = false;
  10.  
  11. $scope.sortHeader = function(column){
  12. $scope.sortReverse = ($scope.sortColumn == column) ? !$scope.sortReverse : false;
  13. $scope.sortColumn = column;
  14.  
  15. }
  16.  
  17. $scope.sortClass = function(column){
  18. if($scope.sortColumn == column){
  19. return $scope.sortReverse ? 'down-arrow' : 'up-arrow';
  20. }
  21. return '';
  22. }
  23. });

There you have it we successfully created a Sorting Table Header 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.