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

Commit b1fa7bb

Browse files
bekospkozlowski-opensource
authored andcommitted
fix(pagination): handle currentPage number as string
1 parent 3e30cd9 commit b1fa7bb

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

src/pagination/docs/demo.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ <h4>Pager</h4>
1515

1616
<hr />
1717
<h4>Limit the maximimum visible page-buttons</h4>
18-
<pagination num-pages="bigNoOfPages" current-page="bigCurrentPage" max-size="maxSize" class="pagination-mini" boundary-links="true"></pagination>
19-
<pagination rotate="false" num-pages="bigNoOfPages" current-page="bigCurrentPage" max-size="maxSize" class="pagination-mini" boundary-links="true"></pagination>
18+
<pagination num-pages="bigNoOfPages" current-page="bigCurrentPage" max-size="maxSize" class="pagination-small" boundary-links="true"></pagination>
19+
<pagination rotate="false" num-pages="bigNoOfPages" current-page="bigCurrentPage" max-size="maxSize" class="pagination-small" boundary-links="true"></pagination>
2020
</div>

src/pagination/pagination.js

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
angular.module('ui.bootstrap.pagination', [])
22

3-
.controller('PaginationController', ['$scope', function (scope) {
3+
.controller('PaginationController', ['$scope', function ($scope) {
44

5-
scope.noPrevious = function() {
6-
return scope.currentPage === 1;
5+
this.currentPage = 1;
6+
7+
this.noPrevious = function() {
8+
return this.currentPage === 1;
79
};
8-
scope.noNext = function() {
9-
return scope.currentPage === scope.numPages;
10+
this.noNext = function() {
11+
return this.currentPage === $scope.numPages;
1012
};
1113

12-
scope.isActive = function(page) {
13-
return scope.currentPage === page;
14+
this.isActive = function(page) {
15+
return this.currentPage === page;
1416
};
1517

16-
scope.selectPage = function(page) {
17-
if ( ! scope.isActive(page) && page > 0 && page <= scope.numPages) {
18-
scope.currentPage = page;
19-
scope.onSelectPage({ page: page });
18+
var self = this;
19+
$scope.selectPage = function(page) {
20+
if ( ! self.isActive(page) && page > 0 && page <= $scope.numPages) {
21+
$scope.currentPage = page;
22+
$scope.onSelectPage({ page: page });
2023
}
2124
};
2225
}])
@@ -43,7 +46,7 @@ angular.module('ui.bootstrap.pagination', [])
4346
controller: 'PaginationController',
4447
templateUrl: 'template/pagination/pagination.html',
4548
replace: true,
46-
link: function(scope, element, attrs) {
49+
link: function(scope, element, attrs, paginationCtrl) {
4750

4851
// Setup configuration parameters
4952
var boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
@@ -66,7 +69,8 @@ angular.module('ui.bootstrap.pagination', [])
6669

6770
scope.$watch('numPages + currentPage + maxSize', function() {
6871
scope.pages = [];
69-
72+
paginationCtrl.currentPage = parseInt(scope.currentPage, 10);
73+
7074
// Default page limits
7175
var startPage = 1, endPage = scope.numPages;
7276
var isMaxSized = ( angular.isDefined(scope.maxSize) && scope.maxSize < scope.numPages );
@@ -75,7 +79,7 @@ angular.module('ui.bootstrap.pagination', [])
7579
if ( isMaxSized ) {
7680
if ( rotate ) {
7781
// Current page is displayed in the middle of the visible ones
78-
startPage = Math.max(scope.currentPage - Math.floor(scope.maxSize/2), 1);
82+
startPage = Math.max(paginationCtrl.currentPage - Math.floor(scope.maxSize/2), 1);
7983
endPage = startPage + scope.maxSize - 1;
8084

8185
// Adjust if limit is exceeded
@@ -85,7 +89,7 @@ angular.module('ui.bootstrap.pagination', [])
8589
}
8690
} else {
8791
// Visible pages are paginated with maxSize
88-
startPage = ((Math.ceil(scope.currentPage / scope.maxSize) - 1) * scope.maxSize) + 1;
92+
startPage = ((Math.ceil(paginationCtrl.currentPage / scope.maxSize) - 1) * scope.maxSize) + 1;
8993

9094
// Adjust last page if limit is exceeded
9195
endPage = Math.min(startPage + scope.maxSize - 1, scope.numPages);
@@ -94,7 +98,7 @@ angular.module('ui.bootstrap.pagination', [])
9498

9599
// Add page number links
96100
for (var number = startPage; number <= endPage; number++) {
97-
var page = makePage(number, number, scope.isActive(number), false);
101+
var page = makePage(number, number, paginationCtrl.isActive(number), false);
98102
scope.pages.push(page);
99103
}
100104

@@ -113,23 +117,23 @@ angular.module('ui.bootstrap.pagination', [])
113117

114118
// Add previous & next links
115119
if (directionLinks) {
116-
var previousPage = makePage(scope.currentPage - 1, previousText, false, scope.noPrevious());
120+
var previousPage = makePage(paginationCtrl.currentPage - 1, previousText, false, paginationCtrl.noPrevious());
117121
scope.pages.unshift(previousPage);
118122

119-
var nextPage = makePage(scope.currentPage + 1, nextText, false, scope.noNext());
123+
var nextPage = makePage(paginationCtrl.currentPage + 1, nextText, false, paginationCtrl.noNext());
120124
scope.pages.push(nextPage);
121125
}
122126

123127
// Add first & last links
124128
if (boundaryLinks) {
125-
var firstPage = makePage(1, firstText, false, scope.noPrevious());
129+
var firstPage = makePage(1, firstText, false, paginationCtrl.noPrevious());
126130
scope.pages.unshift(firstPage);
127131

128-
var lastPage = makePage(scope.numPages, lastText, false, scope.noNext());
132+
var lastPage = makePage(scope.numPages, lastText, false, paginationCtrl.noNext());
129133
scope.pages.push(lastPage);
130134
}
131135

132-
if ( scope.currentPage > scope.numPages ) {
136+
if ( paginationCtrl.currentPage > scope.numPages ) {
133137
scope.selectPage(scope.numPages);
134138
}
135139
});
@@ -174,15 +178,16 @@ angular.module('ui.bootstrap.pagination', [])
174178

175179
scope.$watch('numPages + currentPage', function() {
176180
scope.pages = [];
181+
paginationCtrl.currentPage = parseInt(scope.currentPage, 10);
177182

178183
// Add previous & next links
179-
var previousPage = makePage(scope.currentPage - 1, previousText, scope.noPrevious(), true, false);
184+
var previousPage = makePage(paginationCtrl.currentPage - 1, previousText, paginationCtrl.noPrevious(), true, false);
180185
scope.pages.unshift(previousPage);
181186

182-
var nextPage = makePage(scope.currentPage + 1, nextText, scope.noNext(), false, true);
187+
var nextPage = makePage(paginationCtrl.currentPage + 1, nextText, paginationCtrl.noNext(), false, true);
183188
scope.pages.push(nextPage);
184189

185-
if ( scope.currentPage > scope.numPages ) {
190+
if ( paginationCtrl.currentPage > scope.numPages ) {
186191
scope.selectPage(scope.numPages);
187192
}
188193
});

src/pagination/test/pager.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ describe('pager directive with default configuration', function () {
101101
expect($rootScope.currentPage).toBe(2);
102102
expect($rootScope.selectPageHandler).toHaveBeenCalledWith(2);
103103
});
104+
105+
describe('when `current-page` is not a number', function () {
106+
it('handles string', function() {
107+
$rootScope.currentPage = '1';
108+
$rootScope.$digest();
109+
expect(element.find('li').eq(0).hasClass('disabled')).toBe(true);
110+
111+
$rootScope.currentPage = '05';
112+
$rootScope.$digest();
113+
expect(element.find('li').eq(-1).hasClass('disabled')).toBe(true);
114+
});
115+
});
104116
});
105117

106118
describe('setting pagerConfig', function() {

src/pagination/test/pagination.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,18 @@ describe('pagination directive with default configuration', function () {
115115
expect($rootScope.currentPage).toBe(2);
116116
expect($rootScope.selectPageHandler).toHaveBeenCalledWith(2);
117117
});
118+
119+
describe('when `current-page` is not a number', function () {
120+
it('handles string', function() {
121+
$rootScope.currentPage = '2';
122+
$rootScope.$digest();
123+
expect(element.find('li').eq(2).hasClass('active')).toBe(true);
124+
125+
$rootScope.currentPage = '04';
126+
$rootScope.$digest();
127+
expect(element.find('li').eq(4).hasClass('active')).toBe(true);
128+
});
129+
});
118130
});
119131

120132
describe('pagination directive with max size option', function () {

0 commit comments

Comments
 (0)