Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 8ecf93e

Browse files
bekospkozlowski-opensource
authored andcommitted
fix(pagination): handle extreme values for total-items
Closes #1104
1 parent 53709f0 commit 8ecf93e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/pagination/pagination.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ angular.module('ui.bootstrap.pagination', [])
2727
};
2828

2929
this.calculateTotalPages = function() {
30-
return this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
30+
var totalPages = this.itemsPerPage < 1 ? 1 : Math.ceil($scope.totalItems / this.itemsPerPage);
31+
return Math.max(totalPages || 0, 1);
3132
};
3233

3334
this.getAttributeValue = function(attribute, defaultValue, interpolate) {
@@ -36,7 +37,9 @@ angular.module('ui.bootstrap.pagination', [])
3637

3738
this.render = function() {
3839
this.page = parseInt($scope.page, 10) || 1;
39-
$scope.pages = this.getPages(this.page, $scope.totalPages);
40+
if (this.page > 0 && this.page <= $scope.totalPages) {
41+
$scope.pages = this.getPages(this.page, $scope.totalPages);
42+
}
4043
};
4144

4245
$scope.selectPage = function(page) {

src/pagination/test/pagination.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ describe('pagination directive', function () {
9595
expect(getPaginationEl(-1).text()).toBe('Next');
9696
});
9797

98+
it('does not "break" when `total-items` is undefined', function() {
99+
$rootScope.total = undefined;
100+
$rootScope.$digest();
101+
102+
expect(getPaginationBarSize()).toBe(3); // Previous, 1, Next
103+
expect(getPaginationEl(0)).toHaveClass('disabled');
104+
expect(getPaginationEl(1)).toHaveClass('active');
105+
expect(getPaginationEl(2)).toHaveClass('disabled');
106+
});
107+
108+
it('does not "break" when `total-items` is negative', function() {
109+
$rootScope.total = -1;
110+
$rootScope.$digest();
111+
112+
expect(getPaginationBarSize()).toBe(3); // Previous, 1, Next
113+
expect(getPaginationEl(0)).toHaveClass('disabled');
114+
expect(getPaginationEl(1)).toHaveClass('active');
115+
expect(getPaginationEl(2)).toHaveClass('disabled');
116+
});
117+
98118
it('does not change the current page when `total-items` changes but is valid', function() {
99119
$rootScope.currentPage = 1;
100120
$rootScope.total = 18; // 2 pages
@@ -495,6 +515,15 @@ describe('pagination directive', function () {
495515
expect($rootScope.numpg).toBe(8);
496516
});
497517

518+
it('shows minimun one page if total items are not defined and does not break binding', function() {
519+
$rootScope.total = undefined;
520+
$rootScope.$digest();
521+
expect($rootScope.numpg).toBe(1);
522+
523+
$rootScope.total = 73; // 8 pages
524+
$rootScope.$digest();
525+
expect($rootScope.numpg).toBe(8);
526+
});
498527
});
499528

500529
describe('setting `paginationConfig`', function() {

0 commit comments

Comments
 (0)