Last active
February 13, 2020 07:59
-
-
Save nestorrente/25a20913fe6b5da36ea2d0131c79b63c to your computer and use it in GitHub Desktop.
This function allows you to generate a fully-customizable pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function customizablePaginationAlgorithm(currentPage, totalPages, options = {}) { | |
const { | |
margin = 2, | |
outerMargin = margin, | |
afterFirst = outerMargin, | |
beforeLast = outerMargin, | |
innerMargin = margin, | |
beforeCurrent = innerMargin, | |
afterCurrent = innerMargin, | |
showPageNumberOnSingleElementEllipsis = true, | |
ellipsis = '\u2026' | |
} = options; | |
const result = []; | |
const boundedAfterFirst = Math.min(1 + afterFirst, totalPages); | |
for(let i = 1; i <= boundedAfterFirst; ++i) { | |
result.push({ page: i, ellipsis: false, current: i === currentPage }); | |
} | |
if(boundedAfterFirst === totalPages) { | |
return result; | |
} | |
const boundedBeforeCurrent = Math.max(currentPage - beforeCurrent, boundedAfterFirst + 1); | |
const boundedAfterCurrent = Math.min(currentPage + afterCurrent, totalPages); | |
if(boundedBeforeCurrent > boundedAfterFirst + 1) { | |
if(showPageNumberOnSingleElementEllipsis && boundedBeforeCurrent === boundedAfterFirst + 2) { | |
result.push({ page: boundedAfterFirst + 1, ellipsis: false, current: false }); | |
} else { | |
result.push({ page: ellipsis, ellipsis: true, current: false }); | |
} | |
} | |
for(let i = boundedBeforeCurrent; i <= boundedAfterCurrent; ++i) { | |
result.push({ page: i, ellipsis: false, current: i === currentPage }); | |
} | |
if(boundedAfterCurrent === totalPages) { | |
return result; | |
} | |
const boundedBeforeLast = Math.max(totalPages - beforeLast, boundedAfterCurrent + 1); | |
if(boundedBeforeLast > boundedAfterCurrent + 1) { | |
if(showPageNumberOnSingleElementEllipsis && boundedBeforeLast === boundedAfterCurrent + 2) { | |
result.push({ page: boundedAfterCurrent + 1, ellipsis: false, current: false }); | |
} else { | |
result.push({ page: ellipsis, ellipsis: true, current: false }); | |
} | |
} | |
for(let i = boundedBeforeLast; i <= totalPages; ++i) { | |
result.push({ page: i, ellipsis: false, current: i === currentPage }); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Output: