Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Last active January 2, 2016 22:49
Show Gist options
  • Save mwmitchell/8372145 to your computer and use it in GitHub Desktop.
Save mwmitchell/8372145 to your computer and use it in GitHub Desktop.
pagination-fns.js
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