Paginate HTML Table Using AngularJS

In this tutorial we will create Paginate HTML Table using AngularJS. This code will automatically apply when dirPagination.js is use in the src attribute. Feel free to use this code and modify as you learn from it. To learn more about this, just follow the steps below.

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: This code will only work if run in a local server.

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly 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. <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 well">
  15.                 <h3 class="text-primary">Paginate HTML Table Using AngularJS</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-6">
  18.                         <table class="table table-bordered">
  19.                                 <thead class="alert-info">
  20.                                         <tr>
  21.                                                 <th>ID</th>
  22.                                                 <th>Firstname</th>
  23.                                                 <th>Lastname</th>
  24.                                                 <th>Address</th>
  25.                                         </tr>
  26.                                 </thead>
  27.                                 <tbody>
  28.                                                 <tr dir-paginate="user in users|itemsPerPage:4">
  29.                                                 <td>{{user.id}}</td>
  30.                                                 <td>{{user.firstname}}</td>
  31.                                                 <td>{{user.lastname}}</td>
  32.                                                 <td>{{user.address}}</td>
  33.                                         </tr>
  34.                                 </tbody>
  35.                         </table>
  36.                         <dir-pagination-controls
  37.                                 direction-links="true"
  38.                                 boundary-links="true" >
  39.                         </dir-pagination-controls>
  40.                 </div> 
  41.         </div>
  42. <script src="js/angular.js"></script>
  43. <script src="js/script.js"></script>
  44. <script src="js/dirPagination.js"></script>
  45. </body>
  46. </html>

Creating the Main Function

This code contains the main function of the application. This code will display a clickable rows of number that change the row data when clicked. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below.
  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
  1. [
  2.         {"id":1, "firstname": "Steve", "lastname": "Roger", "address": "New York"},
  3.         {"id":2, "firstname": "Thor", "lastname": "Odinson", "address": "Asgard"},
  4.         {"id":3, "firstname": "Claire", "lastname": "Temple", "address": "Racoon City"},
  5.         {"id":4, "firstname": "Naruto", "lastname": "Uzumaki", "address": "Konoha"},
  6.         {"id":5, "firstname": "Luffy", "lastname": "Monkey", "address": "Grand Blue"},
  7.         {"id":6, "firstname": "Magnolia", "lastname": "Corneto", "address": "Iceland"}
  8. ]
There you have it we successfully created a Paginate HTML Table using AngularJS. 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