Skip to content

Instantly share code, notes, and snippets.

@flavianmissi
Created November 26, 2011 00:46
Show Gist options
  • Select an option

  • Save flavianmissi/1394763 to your computer and use it in GitHub Desktop.

Select an option

Save flavianmissi/1394763 to your computer and use it in GitHub Desktop.
Simple JavaScript pagination
config = {
'items_per_page': 3,
'container_id': 'elements'
};
function Pagination(config, objects) {
this.config = config;
this.objects = objects;
this.current_page = 1;
this.offset = this.config.items_per_page;
}
Pagination.prototype = {
getDataForPage : function () {
var data = [];
for (var i = this.current_page-1; i < this.offset; i++) {
data.push(this.objects[i]);
}
this.current_page += this.config.items_per_page;
this.offset = (this.config.items_per_page + (this.current_page - 1));
return data;
},
next : function () {
var fragment = document.createDocumentFragment();
var data = this.getDataForPage();
var element, container;
for (var i = 0; i < data.length; i++) {
container = document.createElement('div');
for (attr in data[i]) {
element = document.createElement('p');
element.innerHTML = attr + ": " + data[i][attr].toString();
container.appendChild(element);
}
fragment.appendChild(container);
}
document.getElementById(this.config.container_id).appendChild(fragment.cloneNode(true));
},
back : function () {
//incoming
}
};
pagination = new Pagination(config, results);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment