Friday, September 24, 2010

Javascript Pagination with JQuery

This script has been adapted from some other jquery pagination code found on the web.


function Pagination(){
// alert('created')
this.showPerPage = 10;
this.currentPage = 0;
this.lowestPage = 0;
this.highestPage = 2;

}

Pagination.prototype.paginate = function( content ){
this.content = content;
this.numberOfItems = content.children().size();

this.numberOfPages = Math.ceil(this.numberOfItems/this.showPerPage);

var navigation_html = '';
var current_link = 0;
while(this.numberOfPages > current_link){
navigation_html += ''+ (current_link + 1) +'';
current_link++;
}

$('#pages').html(navigation_html);
$( content ).children().css('display', 'none');
$( content ).children().slice( 0, this.showPerPage ).css('display', 'inline');
$('.page_link').css('display','none');
$('.page_link').slice(this.lowestPage, this.highestPage+1 ).css('display','inline')
$('#pages .page_link:first').addClass('active_page');
}


Pagination.prototype.goToPage = function (page_num){

if( page_num==this.lowestPage && page_num!=0 ){
// shift right
this.lowestPage = this.lowestPage - 1;
this.highestPage = this.highestPage -1 ;

}else if( page_num==this.highestPage && page_num // shift left
this.highestPage = this.highestPage + 1;
this.lowestPage = this.lowestPage + 1;
}

var start_from = page_num * this.showPerPage;
var end_on = start_from + this.showPerPage;
this.content.children().css('display', 'none').slice(start_from, end_on).css('display', 'inline');
$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
this.content.val(page_num);

$('.page_link').css('display','none');
$('.page_link').slice(this.lowestPage, this.highestPage+1 ).css('display','inline')
}
]]>



div.gadget h3{font-size:0.7em;font-weight:normal;margin-bottom:2;}
div.gadget p{font-size:0.7em;}
div.footer{position:absolute;bottom:0;display:none;}
div.footer p{line-height:70%;}
#count{font-weight:bold; }
#terms{ overflow:hidden;height:70px;font-size:.7em;overflow-y:auto; }
#terms em{float:right;}
#pages{position:absolute;right:0;bottom:2;}
#pages a{padding:1px;border:1px solid gray;margin:1px;color:black;text-decoration:none;}
.active_page{background:darkblue;color:white !important;}



No comments: