argos/dmp-frontend/src/app/services/pagination.service.ts

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-10-09 15:36:29 +02:00
export class PaginationService {
2018-10-05 17:00:54 +02:00
getPagination(groups, totalGroups: number, currentPage: number = 1, pageSize: number = 3) {
// calculate total pages
//let totalPages = Math.ceil(totalGroups / pageSize);
2017-11-02 15:46:43 +01:00
2018-10-05 17:00:54 +02:00
let totalPages = 0 ; //totalpages based on pages from xml, each group and section has each one page
groups.forEach(group => {
if (group.page > totalPages) {
totalPages = parseInt(group.page);
}
});
2017-11-02 15:46:43 +01:00
2018-10-05 17:00:54 +02:00
let startPage: number, endPage: number;
if (totalPages <= 10) {
// less than 10 total pages so show all
startPage = 1;
endPage = totalPages;
} else {
// more than 10 total pages so calculate start and end pages
if (currentPage <= 6) {
startPage = 1;
endPage = 10;
} else if (currentPage + 4 >= totalPages) {
startPage = totalPages - 9;
endPage = totalPages;
} else {
startPage = currentPage - 5;
endPage = currentPage + 4;
}
}
2017-10-09 15:36:29 +02:00
2018-10-05 17:00:54 +02:00
// calculate start and end item indexes
const startIndex = (currentPage - 1) * pageSize;
const endIndex = Math.min(startIndex + pageSize - 1, totalGroups - 1);
2017-10-09 15:36:29 +02:00
2018-10-05 17:00:54 +02:00
// create an array of pages to ng-repeat in the pagination control
const pages = [];
for (let i = 1; i < endPage + 1; i++) {
pages.push(i);
}
2017-10-09 15:36:29 +02:00
2018-10-05 17:00:54 +02:00
// return object with all pagination properties required by the view
return {
totalGroups: totalGroups,
currentPage: currentPage,
pageSize: pageSize,
totalPages: totalPages,
startPage: startPage,
endPage: endPage,
startIndex: startIndex,
endIndex: endIndex,
pages: pages
};
}
}