Skip to content

Instantly share code, notes, and snippets.

@royhowie
Created July 13, 2015 05:22
Show Gist options
  • Save royhowie/0bd805d197c210bbf3eb to your computer and use it in GitHub Desktop.
Save royhowie/0bd805d197c210bbf3eb to your computer and use it in GitHub Desktop.
pagination page generator
const PAGES_ON_ENDS = 3
function getPages (currentPage, perPage, total) {
let maxPage = Math.floor(total / perPage)
let lower = Math.max(currentPage - PAGES_ON_ENDS, 0)
let upper = Math.min(currentPage + PAGES_ON_ENDS, maxPage)
return Array.from(Array(upper - lower + 1), (_, i) => lower + i)
}
const PAGES_ON_ENDS = 3
function getPages (currentPage, perPage, total) {
let maxPage = Math.floor(total / perPage)
let arr = []
let lower = Math.max(currentPage - PAGES_ON_ENDS, 0)
let upper = Math.min(currentPage + PAGES_ON_ENDS, maxPage)
for (let i = lower; i <= upper; i++) {
arr.push(i)
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment