Last active
January 2, 2016 22:49
-
-
Save mwmitchell/8372145 to your computer and use it in GitHub Desktop.
pagination-fns.js
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 offset(page, itemsPerPage){ | |
return (page - 1) * itemsPerPage; | |
} | |
function maxPages(perPage, totalItems){ | |
return Math.ceil(totalItems / perPage); | |
} | |
function isValidPage(page, itemsPerPage, totalItems){ | |
return ((page > 0) && (page <= maxPages(itemsPerPage, totalItems))); | |
} | |
function pageBack(page){ | |
if(page > 1){ | |
return page - 1; | |
} | |
} | |
function pageForward(page, perPage, totalItems){ | |
var next = page + 1; | |
if(next <= maxPages(perPage, totalItems)){ | |
return next; | |
} | |
} | |
function pageNums(page, perPage, totalItems){ | |
if( isValidPage(page, perPage, totalItems) ){ | |
var N = maxPages(perPage, totalItems); | |
return Array.apply(null, {length: N}).map(Number.call, Number).map( | |
function(i){ | |
return i + page; | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment