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

Commit 56642ea

Browse files
OzzieOrcawesleycho
authored andcommitted
feat(pagination): add force-ellipses option and boundaryLinkNumbers
- Adds force-ellipsis option for adding ellipses to pagination - Add boundary-link-numbers option for allowance of the first and last page numbers to always be shown Closes #2924 Closes #3064 Closes #3565
1 parent 3f307e4 commit 56642ea

File tree

5 files changed

+223
-26
lines changed

5 files changed

+223
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ lib-cov
99
*.swp
1010
*.swo
1111
.DS_Store
12+
.idea
1213

1314
pids
1415
logs

src/pagination/docs/demo.html

+14-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ <h4>Default</h4>
77
<pre>The selected page no: {{currentPage}}</pre>
88
<button type="button" class="btn btn-info" ng-click="setPage(3)">Set current page to: 3</button>
99

10+
<hr />
11+
<h4>Limit the maximum visible buttons</h4>
12+
<h6><code>rotate</code> defaulted to <code>true</code>:</h6>
13+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" num-pages="numPages"></uib-pagination>
14+
<h6><code>rotate</code> defaulted to <code>true</code> and <code>force-ellipses</code> set to <code>true</code>:</h6>
15+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" force-ellipses="true"></uib-pagination>
16+
<h6><code>rotate</code> set to <code>false</code>:</h6>
17+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" rotate="false"></uib-pagination>
18+
<h6><code>boundary-link-numbers</code> set to <code>true</code> and <code>rotate</code> defaulted to <code>true</code>:</h6>
19+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-link-numbers="true"></uib-pagination>
20+
<h6><code>boundary-link-numbers</code> set to <code>true</code> and <code>rotate</code> set to <code>false</code>:</h6>
21+
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-link-numbers="true" rotate="false"></uib-pagination>
22+
<pre>Page: {{bigCurrentPage}} / {{numPages}}</pre>
23+
1024
<hr />
1125
<h4>Pager</h4>
1226
<uib-pager total-items="totalItems" ng-model="currentPage"></uib-pager>
1327

14-
<hr />
15-
<h4>Limit the maximum visible buttons</h4>
16-
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true"></uib-pagination>
17-
<uib-pagination total-items="bigTotalItems" ng-model="bigCurrentPage" max-size="maxSize" class="pagination-sm" boundary-links="true" rotate="false" num-pages="numPages"></uib-pagination>
18-
<pre>Page: {{bigCurrentPage}} / {{numPages}}</pre>
1928
</div>

src/pagination/docs/readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ Settings can be provided as attributes in the `<uib-pagination>` or globally con
3636
* `rotate`
3737
_(Defaults: true)_ :
3838
Whether to keep current page in the middle of the visible ones.
39+
40+
* `force-ellipses`
41+
_(Defaults: false)_ :
42+
Also displays ellipses when `rotate` is true and `max-size` is smaller than the number of pages.
3943

4044
* `direction-links`
4145
_(Default: true)_ :
@@ -60,6 +64,10 @@ Settings can be provided as attributes in the `<uib-pagination>` or globally con
6064
* `last-text`
6165
_(Default: 'Last')_ :
6266
Text for Last button.
67+
68+
* `boundary-link-numbers`
69+
_(Default: false)_ :
70+
Whether to always display the first and last page numbers. If `max-size` is smaller than the number of pages, then the first and last page numbers are still shown with ellipses in-between as necessary. NOTE: `max-size` refers to the center of the range. This option may add up to 2 more numbers on each side of the displayed range for the end value and what would be an ellipsis but is replaced by a number because it is sequential.
6371

6472
* `template-url`
6573
_(Default: 'template/pagination/pagination.html')_ :

src/pagination/pagination.js

+33-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ angular.module('ui.bootstrap.pagination', [])
2424

2525
$scope.$watch('totalItems', function(newTotal, oldTotal) {
2626
if (angular.isDefined(newTotal) || newTotal !== oldTotal) {
27-
$scope.totalPages = self.calculateTotalPages();
27+
$scope.totalPages = self.calculateTotalPages();
2828
updatePage();
2929
}
3030
});
@@ -80,12 +80,14 @@ angular.module('ui.bootstrap.pagination', [])
8080
.constant('uibPaginationConfig', {
8181
itemsPerPage: 10,
8282
boundaryLinks: false,
83+
boundaryLinkNumbers: false,
8384
directionLinks: true,
8485
firstText: 'First',
8586
previousText: 'Previous',
8687
nextText: 'Next',
8788
lastText: 'Last',
88-
rotate: true
89+
rotate: true,
90+
forceEllipses: false
8991
})
9092

9193
.directive('uibPagination', ['$parse', 'uibPaginationConfig', function($parse, paginationConfig) {
@@ -115,7 +117,9 @@ angular.module('ui.bootstrap.pagination', [])
115117

116118
// Setup configuration parameters
117119
var maxSize = angular.isDefined(attrs.maxSize) ? scope.$parent.$eval(attrs.maxSize) : paginationConfig.maxSize,
118-
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate;
120+
rotate = angular.isDefined(attrs.rotate) ? scope.$parent.$eval(attrs.rotate) : paginationConfig.rotate,
121+
forceEllipses = angular.isDefined(attrs.forceEllipses) ? scope.$parent.$eval(attrs.forceEllipses) : paginationConfig.forceEllipses,
122+
boundaryLinkNumbers = angular.isDefined(attrs.boundaryLinkNumbers) ? scope.$parent.$eval(attrs.boundaryLinkNumbers) : paginationConfig.boundaryLinkNumbers;
119123
scope.boundaryLinks = angular.isDefined(attrs.boundaryLinks) ? scope.$parent.$eval(attrs.boundaryLinks) : paginationConfig.boundaryLinks;
120124
scope.directionLinks = angular.isDefined(attrs.directionLinks) ? scope.$parent.$eval(attrs.directionLinks) : paginationConfig.directionLinks;
121125

@@ -148,7 +152,7 @@ angular.module('ui.bootstrap.pagination', [])
148152
if (isMaxSized) {
149153
if (rotate) {
150154
// Current page is displayed in the middle of the visible ones
151-
startPage = Math.max(currentPage - Math.floor(maxSize/2), 1);
155+
startPage = Math.max(currentPage - Math.floor(maxSize / 2), 1);
152156
endPage = startPage + maxSize - 1;
153157

154158
// Adjust if limit is exceeded
@@ -158,7 +162,7 @@ angular.module('ui.bootstrap.pagination', [])
158162
}
159163
} else {
160164
// Visible pages are paginated with maxSize
161-
startPage = ((Math.ceil(currentPage / maxSize) - 1) * maxSize) + 1;
165+
startPage = (Math.ceil(currentPage / maxSize) - 1) * maxSize + 1;
162166

163167
// Adjust last page if limit is exceeded
164168
endPage = Math.min(startPage + maxSize - 1, totalPages);
@@ -172,16 +176,38 @@ angular.module('ui.bootstrap.pagination', [])
172176
}
173177

174178
// Add links to move between page sets
175-
if ( isMaxSized && maxSize > 0 ) {
176-
if ( startPage > 1 ) {
179+
if (isMaxSized && maxSize > 0 && (!rotate || forceEllipses || boundaryLinkNumbers)) {
180+
if (startPage > 1) {
181+
if (!boundaryLinkNumbers || startPage > 3) { //need ellipsis for all options unless range is too close to beginning
177182
var previousPageSet = makePage(startPage - 1, '...', false);
178183
pages.unshift(previousPageSet);
179184
}
185+
if (boundaryLinkNumbers) {
186+
if (startPage === 3) { //need to replace ellipsis when the buttons would be sequential
187+
var secondPageLink = makePage(2, '2', false);
188+
pages.unshift(secondPageLink);
189+
}
190+
//add the first page
191+
var firstPageLink = makePage(1, '1', false);
192+
pages.unshift(firstPageLink);
193+
}
194+
}
180195

181196
if (endPage < totalPages) {
197+
if (!boundaryLinkNumbers || endPage < totalPages - 2) { //need ellipsis for all options unless range is too close to end
182198
var nextPageSet = makePage(endPage + 1, '...', false);
183199
pages.push(nextPageSet);
184200
}
201+
if (boundaryLinkNumbers) {
202+
if (endPage === totalPages - 2) { //need to replace ellipsis when the buttons would be sequential
203+
var secondToLastPageLink = makePage(totalPages - 1, totalPages - 1, false);
204+
pages.push(secondToLastPageLink);
205+
}
206+
//add the last page
207+
var lastPageLink = makePage(totalPages, totalPages, false);
208+
pages.push(lastPageLink);
209+
}
210+
}
185211
}
186212
return pages;
187213
}

0 commit comments

Comments
 (0)