Skip to content

Instantly share code, notes, and snippets.

@adamdehaven
Created March 27, 2019 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamdehaven/161882e49fd06abc9c55dd8962268bb2 to your computer and use it in GitHub Desktop.
Save adamdehaven/161882e49fd06abc9c55dd8962268bb2 to your computer and use it in GitHub Desktop.
Pagination Algorithm
pagination(currentPage, pageCount) {
let delta = 1,
left = currentPage - delta,
right = currentPage + delta + 1,
result = []
result = Array.from({ length: pageCount }, (v, k) => k + 1)
.filter(i => i && i >= left && i < right)
if (result.length > 1) {
// Add first page and dots
if (result[0] > 1) {
if (result[0] > 2) {
// Add dots
result.unshift('...')
}
// Add first page
result.unshift(1)
}
// Add dots and last page
if (result[result.length - 1] < pageCount) {
if (result[result.length - 1] !== pageCount - 1) {
// Add dots
result.push('...')
}
// Add last page
result.push(pageCount)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment