Skip to content

Instantly share code, notes, and snippets.

@cgravolet
Created September 1, 2011 03:57
Show Gist options
  • Select an option

  • Save cgravolet/1185419 to your computer and use it in GitHub Desktop.

Select an option

Save cgravolet/1185419 to your computer and use it in GitHub Desktop.
Pagination controller
(function(vod, $j){
vod.PaginationController = function(){};
$j.extend(vod.PaginationController.prototype, {
attributes: {
endIndex: 1,
itemsPerPage: 25,
itemsPerRow: 5,
startingItemPerPage : 25,
collectionStartItemPerPage : 24,
pageIndex: 1,
pagesPerSet: 5,
rowCount: 5,
startIndex: 1,
totalItems: null,
totalPages: 1
},
calculateEndIndex: function(){
this.attributes.endIndex = this.attributes.startIndex + this.attributes.pagesPerSet - 1;
if (this.attributes.endIndex > this.attributes.totalPages) {
this.attributes.endIndex = this.attributes.totalPages;
}
return this.attributes.endIndex;
}, // calculateEndIndex()
calculateStartIndex: function(){
var startIndex = this.attributes.pageIndex;
if (startIndex % this.attributes.pagesPerSet === 0) {
startIndex--;
}
while (startIndex % this.attributes.pagesPerSet) {
startIndex--;
}
this.attributes.startIndex = startIndex + 1;
return this.attributes.startIndex;
}, // calculateStartIndex()
getItemIndexFromPage: function(page){
return (page * this.attributes.itemsPerPage) - this.attributes.itemsPerPage + 1;
}, // getItemIndexFromPage()
init: function(pageIndex, totalItems, itemsPerPage, pagesPerSet){
this.attributes.pageIndex = parseFloat(pageIndex);
this.attributes.itemsPerPage = parseFloat(itemsPerPage);
this.attributes.totalItems = parseFloat(totalItems);
this.attributes.totalPages = Math.ceil(totalItems / itemsPerPage);
if (pagesPerSet) {
this.attributes.pagesPerSet = pagesPerSet;
}
this.calculateStartIndex();
this.calculateEndIndex();
}, // init()
render: function(element){
var container = element ? $j(element) : null,
paginationArray = [],
paginationStr = 'Page ';
for (var i = this.attributes.startIndex; i <= this.attributes.endIndex; i++) {
if (i == this.attributes.pageIndex) {
paginationArray.push('<strong>' + i + '</strong>');
}
else {
paginationArray.push('<a href="#page' + i + '" class="paginationLink" rel="' + this.getItemIndexFromPage(i) + '">' + i + '</a>');
}
}
/*if (this.attributes.startIndex > 1) {
paginationStr += '<a href="#page' + (this.attributes.startIndex - this.attributes.pagesPerSet) + '" class="paginationLink" rel="' + this.getItemIndexFromPage(this.attributes.startIndex - this.attributes.pagesPerSet) + '">&laquo;</a> ';
}*/
if (this.attributes.pageIndex > 1) {
paginationStr += '<a href="#page1" class="paginationLink" rel="1">&nbsp;&laquo;&nbsp;</a>';
paginationStr += '<a href="#page' + (this.attributes.pageIndex-1) + '" class="paginationLink" rel="' + this.getItemIndexFromPage(this.attributes.pageIndex - 1) + '">&nbsp;&lsaquo;&nbsp;</a> ';
}
paginationStr += paginationArray.join(' | ');
if (this.attributes.pageIndex < this.attributes.totalPages) {
paginationStr += ' <a href="#page' + (this.attributes.pageIndex + 1) + '" class="paginationLink" rel="' + this.getItemIndexFromPage(this.attributes.pageIndex + 1) + '">&nbsp;&rsaquo;&nbsp;</a>';
paginationStr += '<a href="#page' + this.attributes.totalPages + '" class="paginationLink" rel="' + this.getItemIndexFromPage(this.attributes.totalPages) + '">&nbsp;&raquo;&nbsp;</a>';
}
/*if (this.attributes.endIndex < this.attributes.totalPages) {
paginationStr += ' <a href="#page' + (this.attributes.endIndex + 1) + '" class="paginationLink" rel="' + this.getItemIndexFromPage(this.attributes.endIndex + 1) + '">&raquo;</a>';
}*/
if (container) {
container.html(paginationStr);
}
return paginationStr;
}, // render()
renderItemsPerPage: function(pageType, selectedItemPerPage){
var perpageArray = [],
perpage = (pageType == 'collection')? this.attributes.collectionStartItemPerPage:this.attributes.startingItemPerPage,
skipnext = false;
for (var i = 0; i < 3; i++) {
if (skipnext === false) {
if (perpage == parseInt(selectedItemPerPage, 10)) {
perpageArray.push('<strong>' + perpage + '</strong>');
}
else {
perpageArray.push('<a href="#itemsPerPage_' + perpage + '" rel="' + perpage + '" class="itemsperpageLink">' + perpage + '</a>');
}
}
else {
break;
}
if (perpage > this.attributes.totalItems) {
skipnext = true;
}
perpage = perpage + perpage ;
}
if (perpageArray.length > 1) {
return 'Items Per Page: ' + perpageArray.join(' | ');
}
return '';
} // renderItemsPerPage()
});
})(xplat.vod, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment