Merge pull request #1075 from opf/pagination-fix-5639

Cherrypicked everything from pagination fix branch so we can merge this ...
pull/1073/head
Alex Coles 11 years ago
commit 3035c33fe2
  1. 4
      app/assets/javascripts/angular/config/work-packages-config.js
  2. 22
      app/assets/javascripts/angular/directives/components/table_pagination.js
  3. 10
      app/assets/javascripts/angular/services/pagination-service.js
  4. 16
      public/templates/components/table_pagination.html

@ -39,5 +39,7 @@ angular.module('openproject.workPackages.config')
.constant('DEFAULT_PAGINATION_OPTIONS', { .constant('DEFAULT_PAGINATION_OPTIONS', {
page: 1, page: 1,
perPage: 10, perPage: 10,
perPageOptions: [10, 20, 50, 100, 500, 1000] perPageOptions: [10, 20, 50, 100, 500, 1000],
maxVisiblePageOptions: 9,
optionsTruncationSize: 2,
}); });

@ -22,6 +22,7 @@ angular.module('openproject.uiComponents')
PaginationService.setPage(pageNumber); PaginationService.setPage(pageNumber);
updateCurrentRangeLabel(); updateCurrentRangeLabel();
updatePageNumbers();
scope.updateResults(); // update table scope.updateResults(); // update table
}; };
@ -33,7 +34,7 @@ angular.module('openproject.uiComponents')
*/ */
function updateCurrentRangeLabel() { function updateCurrentRangeLabel() {
scope.currentRange = "(" + PaginationService.getLowerPageBound() + " - " + PaginationService.getUpperPageBound(scope.totalEntries) + "/" + scope.totalEntries + ")"; scope.currentRange = "(" + PaginationService.getLowerPageBound() + " - " + PaginationService.getUpperPageBound(scope.totalEntries) + "/" + scope.totalEntries + ")";
} };
/** /**
* @name updatePageNumbers * @name updatePageNumbers
@ -41,12 +42,29 @@ angular.module('openproject.uiComponents')
* @description Defines a list of all pages in numerical order inside the scope * @description Defines a list of all pages in numerical order inside the scope
*/ */
function updatePageNumbers() { function updatePageNumbers() {
var maxVisible = PaginationService.getMaxVisiblePageOptions();
var truncSize = PaginationService.getOptionsTruncationSize();
var pageNumbers = []; var pageNumbers = [];
for (var i = 1; i <= Math.ceil(scope.totalEntries / scope.paginationOptions.perPage); i++) { for (var i = 1; i <= Math.ceil(scope.totalEntries / scope.paginationOptions.perPage); i++) {
pageNumbers.push(i); pageNumbers.push(i);
} }
scope.prePageNumbers = truncatePageNums(pageNumbers, PaginationService.getPage() >= maxVisible, 0, Math.min(PaginationService.getPage() - Math.ceil(maxVisible / 2), pageNumbers.length - maxVisible), truncSize);
scope.postPageNumbers = truncatePageNums(pageNumbers, pageNumbers.length >= maxVisible + (truncSize * 2), maxVisible, pageNumbers.length, 0);
scope.pageNumbers = pageNumbers; scope.pageNumbers = pageNumbers;
} };
function truncatePageNums(pageNumbers, perform, disectFrom, disectLength, truncateFrom){
if (perform){
var tuncationSize = PaginationService.getOptionsTruncationSize();
var truncatedNums = pageNumbers.splice(disectFrom, disectLength);
if (truncatedNums.length >= tuncationSize * 2) truncatedNums.splice(truncateFrom, truncatedNums.length - tuncationSize)
return truncatedNums;
} else {
return [];
}
};
scope.$watch('totalEntries', function() { scope.$watch('totalEntries', function() {
updateCurrentRangeLabel(); updateCurrentRangeLabel();

@ -4,7 +4,9 @@ angular.module('openproject.services')
var paginationOptions = { var paginationOptions = {
page: DEFAULT_PAGINATION_OPTIONS.page, page: DEFAULT_PAGINATION_OPTIONS.page,
perPage: DEFAULT_PAGINATION_OPTIONS.perPage, perPage: DEFAULT_PAGINATION_OPTIONS.perPage,
perPageOptions: DEFAULT_PAGINATION_OPTIONS.perPageOptions perPageOptions: DEFAULT_PAGINATION_OPTIONS.perPageOptions,
maxVisiblePageOptions: DEFAULT_PAGINATION_OPTIONS.maxVisiblePageOptions,
optionsTruncationSize: DEFAULT_PAGINATION_OPTIONS.optionsTruncationSize
}; };
PaginationService = { PaginationService = {
@ -20,6 +22,12 @@ angular.module('openproject.services')
getPerPage: function() { getPerPage: function() {
return paginationOptions.perPage; return paginationOptions.perPage;
}, },
getMaxVisiblePageOptions: function() {
return paginationOptions.maxVisiblePageOptions;
},
getOptionsTruncationSize: function() {
return paginationOptions.optionsTruncationSize;
},
setPerPage: function(perPage) { setPerPage: function(perPage) {
paginationOptions.perPage = perPage; paginationOptions.perPage = perPage;
}, },

@ -6,6 +6,13 @@
href="">Previous href="">Previous
</a> </a>
<span ng-if="prePageNumbers" ng-repeat="pageNumber in prePageNumbers">
<a ng-click="showPage(pageNumber)"
rel="next"
href="">{{ pageNumber }}</a>
</span>
<span ng-if="prePageNumbers" class="gap">...</span>
<span ng-if="pageNumbers.length > 1" ng-repeat="pageNumber in pageNumbers"> <span ng-if="pageNumbers.length > 1" ng-repeat="pageNumber in pageNumbers">
<em ng-if="pageNumber == paginationOptions.page" class="current">{{ pageNumber }}</em> <em ng-if="pageNumber == paginationOptions.page" class="current">{{ pageNumber }}</em>
<a ng-if="pageNumber != paginationOptions.page" <a ng-if="pageNumber != paginationOptions.page"
@ -14,6 +21,13 @@
href="">{{ pageNumber }}</a> href="">{{ pageNumber }}</a>
</span> </span>
<span ng-if="postPageNumbers" class="gap">...</span>
<span ng-if="postPageNumbers" ng-repeat="pageNumber in postPageNumbers">
<a ng-click="showPage(pageNumber)"
rel="next"
href="">{{ pageNumber }}</a>
</span>
<a ng-show="paginationOptions.page != pageNumbers.length" <a ng-show="paginationOptions.page != pageNumbers.length"
ng-click="showPage(paginationOptions.page + 1)" ng-click="showPage(paginationOptions.page + 1)"
class="next_page" class="next_page"
@ -27,7 +41,7 @@
Per page: Per page:
<span ng-repeat="perPageOption in paginationOptions.perPageOptions"> <span ng-repeat="perPageOption in paginationOptions.perPageOptions">
<span ng-if="perPageOption != paginationOptions.perPage"> <span ng-if="perPageOption != paginationOptions.perPage">
<a href="" ng-click="selectPerPage(perPageOption)">{{ perPageOption }}</a> <a href="" ng-click="selectPerPage(perPageOption)">{{ perPageOption }}</a>
</span> </span>
<span ng-if="perPageOption == paginationOptions.perPage" <span ng-if="perPageOption == paginationOptions.perPage"
ng-click="selectPerPage(perPageOption)">{{ perPageOption }}</span> ng-click="selectPerPage(perPageOption)">{{ perPageOption }}</span>

Loading…
Cancel
Save