I am using external pagination with ui-grid and angularjs.
The server has 70 items mocked at the moment, but I will not know the items count from the server. The pageSize is set to 50.
So it should have 2 pages (one with 50 items on it and one with 20).
The button for next page is NOT disabled on the second page but on the third one when there are no items in the grid to show.
I would like it disabled on the second page!
Link to the plunk: http://plnkr.co/edit/OuM8lN0R06Xudn1frLax?p=preview
app.js
var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.pagination']);
app.service('foo', ['$q', function ($q) {
var items = [];
for (var i = 0; i < 70; i++) {
items.push({
"firstName": "firstName" + i,
"lastName": "lastName" + i,
"company": "company" + i,
"employed": i % 4 === 0
});
}
this.bar = function (page, pageSize) {
var deferred = $q.defer();
var start = (page - 1) * pageSize;
deferred.resolve(items.slice(start, start + pageSize));
return deferred.promise;
};
return this;
}]);
app.controller('MainCtrl', ['$scope', '$http', '$log', '$timeout', 'uiGridConstants', 'foo', function ($scope, $http, $log, $timeout, uiGridConstants, foo) {
var paginationOptions = {
pageNumber: 1,
pageSize: 50,
sort: null
};
$scope.selectedIds = [];
$scope.gridOptions = getOptions();
function getOptions() {
return {
enableHorizontalScrollbar: 0,
enableVerticalScrollbar: 2,
enableRowSelection: true,
enableFullRowSelection: true,
multiSelect: true,
paginationPageSizes: [50, 100],
paginationPageSize: 50,
useExternalPagination: true,
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
getPage(1, 50);
gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
paginationOptions.pageNumber = newPage;
paginationOptions.pageSize = pageSize;
getPage(newPage, pageSize);
});
}
};
}
function getPage(page, pageSize) {
foo.bar(page, pageSize)
.then(function (data) {
$scope.gridOptions.data = data;
$scope.gridOptions.totalItems = undefined;
});
}
getPage(1, 50);
}]);
index.html
<!doctype html>
<html ng-app="app">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-touch.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
<script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
<script src="http://ui-grid.info/release/ui-grid.js"></script>
<link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<div ng-controller="MainCtrl">
<div class="grid" ui-grid="gridOptions" ui-grid-pagination ui-grid-selection></div>
</div>
<script src="app.js"></script>
</body>
</html>