AngularJS - Sorting Table Header Source Code

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

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="<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">AngularJS - Sorting Table Header</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-8">
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th ng-click="sortHeader('firstname')">Firstname<div ng-class="sortClass('firstname')"></div></th>
  24. <th ng-click="sortHeader('lastname')">Lastname<div ng-class="sortClass('lastname')"></div></th>
  25. <th ng-click="sortHeader('gender')">Gender<div ng-class="sortClass('gender')"></div></th>
  26. </tr>
  27. </thead>
  28. <tr ng-repeat="member in members | orderBy:sortColumn:sortReverse | filter: search">
  29. <td>{{member.firstname}}</td>
  30. <td>{{member.lastname}}</td>
  31. <td>{{member.address}}</td>
  32. </tr>
  33. </tbody>
  34. </table>
  35. </div>
  36. </div>
  37. <script src="js/angular.js"></script>
  38. <script src="js/script.js"></script>
  39. </body>
  40. </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!!


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.