In this blog, I'm going to show you how to create a simple table like a datatable kind of functionality using AngularJS.
Like jQuery DataTable, my demo also includes,
  1. Sorting By Name
  2. Searching of Data
For that, I have used AngularJS script file as followed.
  1. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
And also, I have used some of the bootstrap script and style files
  1. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  2. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  3. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
Some of the Angular concepts that I have used are listed below.
  • ng-app - Tells that it is a root element of AngularJs Application
  • ng-controller - Invokes by $Scope
  • ng-model - Binds input field to a variable
  • ng-click - Defines what we should do when an element is clicked
Here, you can find the source code of what I have developed.
  1. <body>
  2. <div class="container">
  3. <div ng-app="myApp" ng-controller="namesCtrl" ng-init="IsReverse=false">
  4. Search: <input type="text" ng-model="test"><br>
  5. <table class="table table-hover table-bordered table-striped">
  6. <tr>
  7. <th ng-click="sort('Name')">Name
  8. <th ng-click="sort('Age')">Age
  9. <th ng-click="sort('Email')">Email
  10. <th>Actions</th>
  11. </tr>
  12. <tr ng-repeat="x in names | filter:test | orderBy:sortParam:IsReverse">
  13. <td>{{x.Name}}
  14. <td>{{x.Age}}
  15. <td>{{x.Email}}
  16. <td>
  17. <div class="btn-group">
  18. <a class="btn btn-primary" href="#">EDIT</a>
  19. <a class="btn btn-primary" href="#">DELETE</a>
  20. </div>
  21. </td>
  22. </tr>
  23. </table>
  24. </div>
  25. <script>
  26. angular.module('myApp', []).controller('namesCtrl', function($scope) {
  27. $scope.names = [
  28. {Name:'Manav',Age:'22',Email:'[email protected]'},
  29. {Name:'Rahul',Age:'25',Email:'[email protected]'},
  30. {Name:'Rohan',Age:'28',Email:'[email protected]'},
  31. {Name:'Jinish',Age:'18',Email:'[email protected]'}
  32. ];
  33. $scope.sort = function(sortId) {
  34. $scope.sortParam = sortId;
  35. if($scope.IsReverse)
  36. {
  37. $scope.IsReverse = false;
  38. }
  39. else
  40. {
  41. $scope.IsReverse = true;
  42. }
  43. };
  44. });
  45. </script>
  46. </body>
As you can see, the Controller will be invoked by calling Using.
  1. controller('namesCtrl', function($scope)
Primary output of my code is as follows:
And for searching , you can see output after I have searched "Manav;" the data will be filtered :
That's all for now. You can try and enhance it by adding more functionality like pagination or total item count etc.
Thanks for reading.