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.                                 <thead>
  19.                                         <tr>
  20.                                                 <th ng-click="sort('firstname')">Firstname
  21.                                                         <span class="glyphicon sort-icon" ng-show="sortKey=='firstname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  22.                                                 </th>
  23.                                                 <th ng-click="sort('lastname')">Lastname
  24.                                                         <span class="glyphicon sort-icon" ng-show="sortKey=='lastname'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  25.                                                 </th>
  26.                                                 <th ng-click="sort('gender')">Gender
  27.                                                         <span class="glyphicon sort-icon" ng-show="sortKey=='gender'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  28.                                                 </th>
  29.                                                 <th ng-click="sort('address')">Address
  30.                                                         <span class="glyphicon sort-icon" ng-show="sortKey=='address'" ng-class="{'glyphicon-chevron-up':reverse,'glyphicon-chevron-down':!reverse}"></span>
  31.                                                 </th>
  32.                                         </tr>
  33.                                 </thead>
  34.                                 <tbody>
  35.                                         <tr dir-paginate="member in members|orderBy:'lastname'|orderBy:sortKey:reverse|itemsPerPage:5">
  36.                                                 <td>{{member.firstname}}</td>
  37.                                                 <td>{{member.lastname}}</td>
  38.                                                 <td>{{member.gender}}</td>
  39.                                                 <td>{{member.address}}</td>
  40.                                         </tr>
  41.                                 </tbody>
  42.                         </table>
  43.                         <dir-pagination-controls
  44.                                 direction-links="true"
  45.                                 boundary-links="true" >
  46.                         </dir-pagination-controls>
  47.                 </div>
  48.         </div>
  49. <script src="js/angular.js"></script>
  50. <script src="js/dirPagination.js"></script>
  51. <script src="js/script.js"></script>
  52. </body>
  53. </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!!

Add new comment