JavaScript - Pagination Array Using AngularJS

In this tutorial we will create a Pagination Array using AngularJS. This code will apply the pagination script when the web is being open. The code use an AngularJS plugin dirPagination an advance plugin that can change your regular HTML table into a dynamic user interactive content when used. 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.         <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 well">
  14.                 <h3 class="text-primary">JavaScript - Pagination Array Using AngularJS</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-7">
  17.                         <div ng-controller="myController">
  18.                                 <table class="table table-bordered">
  19.                                         <thead class="alert-info">
  20.                                                 <tr>
  21.                                                         <th>Firstname</th>
  22.                                                         <th>Lastname</th>
  23.                                                         <th>Address</th>
  24.                                                 </tr>
  25.                                         </thead>
  26.                                         <tbody>
  27.                                                 <tr dir-paginate="member in members|orderBy:'lastname'|itemsPerPage:4">
  28.                                                         <td>{{member.firstname}}</td>
  29.                                                         <td>{{member.lastname}}</td>
  30.                                                         <td>{{member.address}}</td>
  31.                                                 </tr>
  32.                                         </tbody>
  33.                                 </table>
  34.                                 <dir-pagination-controls
  35.                                         direction-links="true"
  36.                                         boundary-links="true" >
  37.                                 </dir-pagination-controls>
  38.                         </div>
  39.                 </div>
  40.         </div>
  41. <script src="js/angular.js"></script>
  42. <script src="js/dirPagination.js"></script>
  43. <script src="js/script.js"></script>
  44. </body>
  45. </html>

Creating the Main Function

This code contains the main function of the application. This code will automatically apply to your table properties when applied. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory.
  1. var app = angular.module('myModule', ['angularUtils.directives.dirPagination']);
  2.  
  3. app.controller('myController', function($scope){
  4.         var members = [
  5.                 {firstname: "Jason", lastname: "Smith", address: "USA"},
  6.                 {firstname: "Mraz", lastname: "Cullen", address: "Tokyo"},
  7.                 {firstname: "Abida", lastname: "Mercy", address: "Germany"},
  8.                 {firstname: "Steve", lastname: "Roger", address: "China"},
  9.                 {firstname: "Claire", lastname: "Temple", address: "Belgum"}
  10.         ];
  11.        
  12.         $scope.members = members;
  13.  
  14. });
  15. </code>
  16. There you have it we successfully created a <strong>Pagination Array</strong> 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