AngularJS Data Grid with Paging
created by ozkary.com
by ozkary
HTML
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.0/ui-bootstrap-tpls.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<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>
...
JavaScript
(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);
}
...