Skip to content

Instantly share code, notes, and snippets.

@jehugaleahsa
Created February 15, 2012 17:29
Show Gist options
  • Save jehugaleahsa/1837472 to your computer and use it in GitHub Desktop.
Save jehugaleahsa/1837472 to your computer and use it in GitHub Desktop.
Pagination
// page: the currently selected page (1 <= page <= pageCount)
// pageCount: the total number of pages (1 <= pageCount)
// pagesToDisplay: the number of pages displayed at a time (1 <= pagesToDisplay)
function paginate(page, pageCount, pagesToDisplay) {
var buffer = Math.floor(pagesToDisplay / 2);
return {
start: Math.max(1, Math.min(page - buffer, pageCount - pagesToDisplay)),
stop: Math.min(pageCount, Math.max(page + buffer, pagesToDisplay))
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment