Skip to main content

JavaScript - Simple Pagination Using AngularJS


In this tutorial we will create a Simple Pagination Using AngularJS using AngularJS. AngularJS is a structural framework for dynamic web apps. It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

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/.

Lastly, this is the link for AngularJS module https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination

Note: You need to download this module to make the pagination work on your application. Make sure you put the dirPagination.js in your HTML file

The Main Interface

This code contains the interface of the application. This code will render application and display the form. To create this just 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 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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodster</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6">
  14. <h3 class="text-primary">JavaScript - Simple Pagination Using AngularJS</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div ng-controller="myController">
  17. <table class="table table-bordered">
  18. <tr>
  19. <th ng-click="sort('firstname')">Firstname
  20. <span class="glyphicon sort-icon" ng-show="sortKey=='firstname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  21. </th>
  22. <th ng-click="sort('lastname')">Lastname
  23. <span class="glyphicon sort-icon" ng-show="sortKey=='lastname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  24. </th>
  25. <th ng-click="sort('gender')">Gender
  26. <span class="glyphicon sort-icon" ng-show="sortKey=='gender'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  27. </th>
  28. <th ng-click="sort('address')">Address
  29. <span class="glyphicon sort-icon" ng-show="sortKey=='address'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  30. </th>
  31. </tr>
  32. </thead>
  33. <tr dir-paginate="member in members|orderBy:'lastname'|orderBy:sortKey:reverse|itemsPerPage:5">
  34. <td>{{member.firstname}}</td>
  35. <td>{{member.lastname}}</td>
  36. <td>{{member.gender}}</td>
  37. <td>{{member.address}}</td>
  38. </tr>
  39. </tbody>
  40. </table>
  41. <dir-pagination-controls
  42. direction-links="true"
  43. boundary-links="true" >
  44. </dir-pagination-controls>
  45. </div>
  46. </div>
  47. <script src="js/angular.js"></script>
  48. <script src="js/dirPagination.js"></script>
  49. <script src="js/script.js"></script>
  50. </body>
  51. </html>

Creating the Script

This code contains the script of the application. This code will automatically apply the pagination to your table. To do this just copy write the code as shown below inside the text editor and save it as script.js.

  1. var app = angular.module('myModule', ['angularUtils.directives.dirPagination']);
  2.  
  3. app.controller('myController', function($scope){
  4. var members = [
  5. {firstname: "Jason", lastname: "Smith", gender: "Male", address: "USA"},
  6. {firstname: "Mraz", lastname: "Cullen", gender: "Male", address: "Tokyo"},
  7. {firstname: "Abida", lastname: "Mercy", gender: "Male", address: "Germany"},
  8. {firstname: "Peter", lastname: "Pan", gender: "Male", address: "Netherland"},
  9. {firstname: "Mickey", lastname: "Moose", gender: "Male", address: "Thailan"},
  10. {firstname: "Jenny", lastname: "Cursek", gender: "Female", address: "Korea"},
  11. {firstname: "Likan", lastname: "Braz", gender: "Female", address: "USA"}
  12. ];
  13.  
  14. $scope.members = members;
  15. $scope.sort = function(keyword){
  16. $scope.sortKey = keyword;
  17. $scope.reverse = !$scope.reverse;
  18. }
  19. });

There you have it we successfully created a Simple Pagination Using AngularJS using JavaScript. 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.