JavaScript - Simple Pagination Using AngularJS

In this tutorial we will create a Simple Pagination Using AngularJS. This code will automatically apply the pagination script when the application have been launch. The code use an AngularJS plugin dirPagination an advance plugin that can change your regular table into a dynamic readable content when applied. 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 have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6">
  15. <h3 class="text-primary">JavaScript - Simple Pagination Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <table class="table table-bordered">
  18. <tr>
  19. <th>ID</th>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Address</th>
  23. </tr>
  24. </thead>
  25. <tr dir-paginate="user in users|orderBy:'lastname'|itemsPerPage:4">
  26. <td>{{user.id}}</td>
  27. <td>{{user.firstname}}</td>
  28. <td>{{user.lastname}}</td>
  29. <td>{{user.address}}</td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. <dir-pagination-controls
  34. direction-links="true"
  35. boundary-links="true" >
  36. </dir-pagination-controls>
  37. </div>
  38. <script src="js/angular.js"></script>
  39. <script src="js/script.js"></script>
  40. <script src="js/dirPagination.js"></script>
  41. </body>
  42. </html>

Creating the Main Function

This code contains the main function of the application. This code will overwrite your table properties into an advance pagination tool. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below. script.js Note:Make sure you save this file inside the js directory.
  1. var app = angular.module('myModule', ['angularUtils.directives.dirPagination'])
  2. .controller('myController', function($scope, $http){
  3. $http.get('data.json').then(function(response){
  4. $scope.users = response.data;
  5. });
  6. });
data.json Note: This contains a list of data that use in the tutorial.
  1. [
  2. {"id":1, "firstname": "John", "lastname": "Smith", "address": "New York"},
  3. {"id":2, "firstname": "Edward", "lastname": "Jam", "address": "Russia"},
  4. {"id":3, "firstname": "Claire", "lastname": "Temple", "address": "Racoon City"},
  5. {"id":4, "firstname": "Margat", "lastname": "Hulado", "address": "Spain"},
  6. {"id":5, "firstname": "Vic", "lastname": "Culto", "address": "Germany"},
  7. {"id":6, "firstname": "Magnolia", "lastname": "Corneto", "address": "Iceland"}
  8. ]
There you have it we successfully created a Simple Pagination using AngularJS. I hope that this 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