(function (angular) { 'use strict'; var appName = 'app'; var app = angular.module(appName, ['ui.bootstrap']); app.filter("pagingFilter", function () { return function (items, page, onPage) { var pageSize = parseInt(onPage) if (items.length <= pageSize) return items; var offset = (page - 1) * pageSize; var end = offset + pageSize; var data = items.slice(offset, end); return data; }; }); app.factory('service.paging', [pagingService]); app.controller('app.grid', ['$scope', 'service.paging', gridCtrl]); function pagingService() { return { pageSize: 5, currentPage: 1, totalPages: 0, totalRecords: 0, setOnPage: function () { this.update(this.totalRecords); }, getOnPage: function () { return this.pageSize; }, update: function (len) { this.totalPages = Math.ceil(len / this.pageSize); this.currentPage = 1; }, next: function () { if (this.totalPages == this.currentPage) { return true; } this.currentPage++; }, prev: function () { if (this.currentPage == 1) { return true; } this.currentPage--; }, isFirstPage: function () { return (this.currentPage == 1); }, isLastPage: function () { return (this.currentPage == this.totalPages); }, init: function (pageSize, data) { this.pageSize = parseInt(pageSize); if (data && data.length > 0) { this.totalRecords = data.length; this.update(this.totalRecords); } return this; } }; } function gridCtrl($scope, pager) { var ctrl = this; ctrl.items = [{ year: 1999, make: 'Nissan', model: 'Altima' }, { year: 1999, make: 'Nissan', model: 'Cube' }, { year: 2011, make: 'Nissan', model: 'Rogue' }, { year: 2011, make: 'Nissan', model: 'xTerra' }, { year: 2012, make: 'Nissan', model: 'Altima' }, { year: 2012, make: 'Nissan', model: 'Cube' }, { year: 2012, make: 'Nissan', model: 'Rogue' }, { year: 2012, make: 'Nissan', model: 'xTerra' }, { year: 1999, make: 'Toyota', model: 'Corolla' }, { year: 1999, make: 'Toyota', model: 'Yaris' }, { year: 2012, make: 'Toyota', model: 'Camry' }, { year: 2012, make: 'Toyota', model: 'Rav4' }, { year: 2013, make: 'Toyota', model: 'Corolla' }, { year: 2014, make: 'Toyota', model: 'Yaris' }, { year: 2014, make: 'Toyota', model: 'Camry' }, { year: 2014, make: 'Toyota', model: 'Rav4' }]; ctrl.pager = pager.init(5, ctrl.items); } })(angular);
<div ng-app="app"> <div ng-controller="app.grid as ctrl" class="container text-center"> <h4>Data Grid with Pagination Demo</h4> <table class="table table-condense"> <caption>ozkary.com Vehicle Inventory</caption> <thead> <tr class="bg-primary"> <th>Year</th> <th>Make</th> <th>Model</th> </tr> </thead> <tbody> <tr ng-repeat="item in ctrl.items | pagingFilter:ctrl.pager.currentPage:ctrl.pager.pageSize"> <td>{{item.year}}</td> <td>{{item.make}}</td> <td>{{item.model}}</td> </tr> </tbody> <tfoot> <tr> <td colspan="3"> <ul class="pager form-inline col-xs-12 text-center"> <li> <button type="button" class="btn" ng-click="ctrl.pager.prev()" ng-disabled="ctrl.pager.isFirstPage()"><i class="fa fa-arrow-left"></i> </button> </li> <li> <button type="button" class="btn" ng-click="ctrl.pager.next()" ng-disabled="ctrl.pager.isLastPage()"> <i class="fa fa-arrow-right"></i> </button> </li> <li> <select focus-item="{x:3, y:4}" class="no-print form-control small" ng-model="ctrl.pager.pageSize" ng-change="ctrl.pager.setOnPage()"> <option value="5">5</option> <option value="10">10</option> </select> </li> <li> <strong>Page {{ctrl.pager.currentPage}} of {{ctrl.pager.totalPages}}</strong> Total Records {{ctrl.items.length}}</li> </ul> </td> </tr> </tfoot> </table> </div> </div>