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.
<!DOCTYPE html>
<html lang="en" ng-app="myModule"> <meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1"/> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> <body ng-controller="myController"> <nav class="navbar navbar-default">
<div class="container-fluid"> <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester
</a> </nav>
<h3 class="text-primary">JavaScript - Simple Pagination Using AngularJS
</h3> <hr style="border-top:1px dotted #ccc;"/> <table class="table table-bordered"> <tr dir-paginate="user in users|orderBy:'lastname'|itemsPerPage:4"> <td>{{user.firstname}}
</td> <td>{{user.lastname}}
</td> <td>{{user.address}}
</td> <dir-pagination-controls
direction-links="true"
boundary-links="true" >
</dir-pagination-controls>
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.
var app = angular.module('myModule', ['angularUtils.directives.dirPagination'])
.controller('myController', function($scope, $http){
$http.get('data.json').then(function(response){
$scope.users = response.data;
});
});
data.json
Note: This contains a list of data that use in the tutorial.
[
{"id":1, "firstname": "John", "lastname": "Smith", "address": "New York"},
{"id":2, "firstname": "Edward", "lastname": "Jam", "address": "Russia"},
{"id":3, "firstname": "Claire", "lastname": "Temple", "address": "Racoon City"},
{"id":4, "firstname": "Margat", "lastname": "Hulado", "address": "Spain"},
{"id":5, "firstname": "Vic", "lastname": "Culto", "address": "Germany"},
{"id":6, "firstname": "Magnolia", "lastname": "Corneto", "address": "Iceland"}
]
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!!