Forum Moderators: open

Message Too Old, No Replies

Add a range to a jQuery pagination?

         

keitai

8:56 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



Hello,

i followed the pagination from <snipped url>

and now i like to add a range
So instead of [2][3][4][5][6][7]

i like something [1][2][3][>][last]

the .js code i am using right now is


function paginateIt() {
$("#pagination-ajax").remove();
$('table#overviewJobs').each(function() {

var currentPage = 0;
var numPerPage = 7;
var $table = $(this);

$table.bind('repaginate', function() {

$table.find('tbody tr').show()

.slice(0,currentPage * numPerPage)
.hide()
.end()

.slice((currentPage + 1) * numPerPage)
.hide()
.end();

});

var numRows = $table.find('tbody tr').length;
//alert("Num of rows "+numRows);
var numPages = Math.ceil(numRows / numPerPage);

var $pager = $('<div id="pagination-ajax"></div>');

if(numRows > numPerPage) {
for (var page = 0; page < numPages; page++) {
$('<span class="page-number">' + (page + 1) + '</span>')
.bind('click', {'newPage': page}, function(event) {
currentPage = event.data['newPage'];
//numPerPage = 7;
$table.trigger('repaginate');
$(this).addClass('active').siblings().removeClass('active');
})
.appendTo($pager).addClass('clickable');
}
$pager.find('span.page-number:first').addClass('active');
$pager.insertAfter($('#pagination-divider'));
} //end if numRows
//
//$('#pagination').html(navigation_html);
$table.trigger('repaginate');

});
};



Any help?/

[1][edited by: whoisgregg at 5:05 pm (utc) on Mar 22, 2010]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] :) [/edit]

whoisgregg

1:23 pm on Apr 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a simple approach to get you started. Inside of this loop:

for (var page = 0; page < numPages; page++) { 

You wrap the whole logic with something like this:

if ( Math.abs(currentPage - page) <= 1 ) { 

Then when they are on page 4, it will only show links to pages 3, 4, and 5. However, you'll find that this only gives two links when they are on the first or last page. To fix those, you just add additional statements for those scenarios:

if ( Math.abs(currentPage - page) <= 1 || (currentPage = 0 && page == 2) || (currentPage = numPages && page == (numPages - 2))) {