Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created October 23, 2012 14:15
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 bloodyowl/3938983 to your computer and use it in GitHub Desktop.
Save bloodyowl/3938983 to your computer and use it in GitHub Desktop.
JS pagination
// requires Array.range mixin
function paginate(list, itemsPerPage){
// @list : Array, what to paginate
// @itemsPerPage : Number
var fragment = DOM.createFragment();
Array.range(0, Math.ceil(list.length / itemsPerPage)).forEach(function(item){
var page = DOM.create("div").setAttr("id", "page-" + item).appendTo(fragment);
if(item != 0) page.addClass("hidden");
Array.range(itemsPerPage * item, itemsPerPage * item + itemsPerPage).forEach(function(i){
if(list[i] == null) return;
page.insert({ bottom : list[i]})
})
})
return fragment
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment