NOTE! The following are not part of the original plugin:

Triggered Events

Demo




First Prev 1 to 10 (50) Next Last
Name
Major
Sex
English
Japanese
Calculus
Geometry
Name Major Sex English Japanese Calculus Geometry
Student01 Languages male 80 70 75 80
Student02 Mathematics male 90 88 100 90
Student03 Languages female 85 95 80 85
Student04 Languages male 60 55 100 100
Student05 Languages female 68 80 95 80
Student06 Mathematics male 100 99 100 90
Student07 Mathematics male 85 68 90 90
Student08 Languages male 100 90 90 85
Student09 Mathematics male 80 50 65 75
Student10 Languages male 85 100 100 90
First Prev 1 to 10 (50) Next Last

Javascript

$(function(){

// **********************************
// Description of ALL pager options
// **********************************
var pagerOptions = {

// target the pager markup - see the HTML block below
container: $(".pager"),

// use this url format "http:/mydatabase.com?page={page}&size={size}&{sortList:col}"
ajaxUrl: null,

// modify the url after all processing has been applied
customAjaxUrl: function(table, url) { return url; },

// process ajax so that the data object is returned along with the total number of rows
// example: { "data" : [{ "ID": 1, "Name": "Foo", "Last": "Bar" }], "total_rows" : 100 }
ajaxProcessing: function(ajax){
if (ajax && ajax.hasOwnProperty('data')) {
// return [ "data", "total_rows" ];
return [ ajax.total_rows, ajax.data ];
}
},

// output string - default is '{page}/{totalPages}'
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
output: '{startRow} to {endRow} ({totalRows})',

// apply disabled classname to the pager arrows when the rows at either extreme is visible - default is true
updateArrows: true,

// starting page of the pager (zero based index)
page: 0,

// Number of visible rows - default is 10
size: 10,

// Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js)
savePages : true,

//defines custom storage key
storageKey:'tablesorter-pager',

// if true, the table will remain the same height no matter how many records are displayed. The space is made up by an empty
// table row set to a height to compensate; default is false
fixedHeight: true,

// remove rows from the table to speed up the sort of large tables.
// setting this to false, only hides the non-visible rows; needed if you plan to add/remove rows with the pager enabled.
removeRows: false,

// css class names of pager arrows
cssNext: '.next', // next page arrow
cssPrev: '.prev', // previous page arrow
cssFirst: '.first', // go to first page arrow
cssLast: '.last', // go to last page arrow
cssGoto: '.gotoPage', // select dropdown to allow choosing a page

cssPageDisplay: '.pagedisplay', // location of where the "output" is displayed
cssPageSize: '.pagesize', // page size selector - select dropdown that sets the "size" option

// class added to arrows when at the extremes (i.e. prev/first arrows are "disabled" when on the first page)
cssDisabled: 'disabled', // Note there is no period "." in front of this class name
cssErrorRow: 'tablesorter-errorRow' // ajax error information row

};

$("table")

// Initialize tablesorter
// ***********************
.tablesorter({
theme: 'blue',
widthFixed: true,
widgets: ['zebra']
})

// bind to pager events
// *********************
.bind('pagerChange pagerComplete pagerInitialized pageMoved', function(e, c){
var msg = '"</span> event triggered, ' + (e.type === 'pagerChange' ? 'going to' : 'now on') +
' page <span class="typ">' + (c.page + 1) + '/' + c.totalPages + '</span>';
$('#display')
.append('<li><span class="str">"' + e.type + msg + '</li>')
.find('li:first').remove();
})

// initialize the pager plugin
// ****************************
.tablesorterPager(pagerOptions);

// Add two new rows using the "addRows" method
// the "update" method doesn't work here because not all rows are
// present in the table when the pager is applied ("removeRows" is false)
// ***********************************************************************
var r, $row, num = 50,
row = '<tr><td>Student{i}</td><td>{m}</td><td>{g}</td><td>{r}</td><td>{r}</td><td>{r}</td><td>{r}</td><td><button type="button" class="remove" title="Remove this row">X</button></td></tr>' +
'<tr><td>Student{j}</td><td>{m}</td><td>{g}</td><td>{r}</td><td>{r}</td><td>{r}</td><td>{r}</td><td><button type="button" class="remove" title="Remove this row">X</button></td></tr>';
$('button:contains(Add)').click(function(){
// add two rows of random data!
r = row.replace(/\{[gijmr]\}/g, function(m){
return {
'{i}' : num + 1,
'{j}' : num + 2,
'{r}' : Math.round(Math.random() * 100),
'{g}' : Math.random() > 0.5 ? 'male' : 'female',
'{m}' : Math.random() > 0.5 ? 'Mathematics' : 'Languages'
}[m];
});
num = num + 2;
$row = $(r);
$('table')
.find('tbody').append($row)
.trigger('addRows', [$row]);
return false;
});

// Delete a row
// *************
$('table').delegate('button.remove', 'click' ,function(){
var t = $('table');
// disabling the pager will restore all table rows
// t.trigger('disable.pager');
// remove chosen row
$(this).closest('tr').remove();
// restore pager
// t.trigger('enable.pager');
t.trigger('update');
return false;
});

// Destroy pager / Restore pager
// **************
$('button:contains(Destroy)').click(function(){
// Exterminate, annhilate, destroy! http://www.youtube.com/watch?v=LOqn8FxuyFs
var $t = $(this);
if (/Destroy/.test( $t.text() )){
$('table').trigger('destroy.pager');
$t.text('Restore Pager');
} else {
$('table').tablesorterPager(pagerOptions);
$t.text('Destroy Pager');
}
return false;
});

// Disable / Enable
// **************
$('.toggle').click(function(){
var mode = /Disable/.test( $(this).text() );
$('table').trigger( (mode ? 'disable' : 'enable') + '.pager');
$(this).text( (mode ? 'Enable' : 'Disable') + 'Pager');
return false;
});
$('table').bind('pagerChange', function(){
// pager automatically enables when table is sorted.
$('.toggle').text('Disable Pager');
});

});

CSS

/* pager wrapper, div */
.tablesorter-pager {
padding: 5px;
}
/* pager wrapper, in thead/tfoot */
td.tablesorter-pager {
background-color: #e6eeee;
margin: 0; /* needed for bootstrap .pager gets a 18px bottom margin */
}
/* pager navigation arrows */
.tablesorter-pager img {
vertical-align: middle;
margin-right: 2px;
cursor: pointer;
}

/* pager output text */
.tablesorter-pager .pagedisplay {
padding: 0 5px 0 5px;
width: 50px;
text-align: center;
}

/* pager element reset (needed for bootstrap) */
.tablesorter-pager select {
margin: 0;
padding: 0;
}

/*** css used when "updateArrows" option is true ***/
/* the pager itself gets a disabled class when the number of rows is less than the size */
.tablesorter-pager.disabled {
display: none;
}
/* hide or fade out pager arrows when the first or last row is visible */
.tablesorter-pager .disabled {
/* visibility: hidden */
opacity: 0.5;
filter: alpha(opacity=50);
cursor: default;
}

HTML

<table class="tablesorter">
<!-- view page source to see the entire table -->
</table>

<!-- pager -->
<div id="pager" class="pager">
<form>
<img src="first.png" class="first"/>
<img src="prev.png" class="prev"/>
<span class="pagedisplay"></span> <!-- this can be any element, including an input -->
<img src="next.png" class="next"/>
<img src="last.png" class="last"/>
<select class="pagesize">
<option selected="selected" value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
</select>
</form>
</div>

Pager Change Log


Next up: Pager plugin - Ajax ››