2012-11-28 02:55:36 +00:00
|
|
|
/*!
|
|
|
|
* tablesorter pager plugin
|
2013-05-09 04:36:06 +00:00
|
|
|
* updated 5/8/2013
|
2012-11-28 02:55:36 +00:00
|
|
|
*/
|
|
|
|
/*jshint browser:true, jquery:true, unused:false */
|
|
|
|
;(function($) {
|
|
|
|
"use strict";
|
2012-12-18 21:50:42 +00:00
|
|
|
/*jshint supernew:true */
|
|
|
|
$.extend({ tablesorterPager: new function() {
|
2012-11-28 02:55:36 +00:00
|
|
|
|
|
|
|
this.defaults = {
|
|
|
|
// target the pager markup
|
|
|
|
container: null,
|
|
|
|
|
2012-12-17 17:33:46 +00:00
|
|
|
// use this format: "http://mydatabase.com?page={page}&size={size}&{sortList:col}&{filterList:fcol}"
|
|
|
|
// where {page} is replaced by the page number, {size} is replaced by the number of records to show,
|
|
|
|
// {sortList:col} adds the sortList to the url into a "col" array, and {filterList:fcol} adds
|
|
|
|
// the filterList to the url into an "fcol" array.
|
2012-11-28 02:55:36 +00:00
|
|
|
// So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
|
2012-12-17 17:33:46 +00:00
|
|
|
// and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
|
2012-11-28 02:55:36 +00:00
|
|
|
ajaxUrl: null,
|
|
|
|
|
2013-03-28 01:01:39 +00:00
|
|
|
// modify the url after all processing has been applied
|
2013-03-11 16:14:17 +00:00
|
|
|
customAjaxUrl: function(table, url) { return url; },
|
|
|
|
|
2013-05-08 22:02:36 +00:00
|
|
|
// modify the $.ajax object to allow complete control over your ajax requests
|
|
|
|
ajaxObject: {
|
|
|
|
dataType: 'json'
|
|
|
|
},
|
|
|
|
|
2012-11-28 02:55:36 +00:00
|
|
|
// process ajax so that the following information is returned:
|
|
|
|
// [ total_rows (number), rows (array of arrays), headers (array; optional) ]
|
|
|
|
// example:
|
|
|
|
// [
|
|
|
|
// 100, // total rows
|
|
|
|
// [
|
|
|
|
// [ "row1cell1", "row1cell2", ... "row1cellN" ],
|
|
|
|
// [ "row2cell1", "row2cell2", ... "row2cellN" ],
|
|
|
|
// ...
|
|
|
|
// [ "rowNcell1", "rowNcell2", ... "rowNcellN" ]
|
|
|
|
// ],
|
|
|
|
// [ "header1", "header2", ... "headerN" ] // optional
|
|
|
|
// ]
|
|
|
|
ajaxProcessing: function(ajax){ return [ 0, [], null ]; },
|
|
|
|
|
|
|
|
// output default: '{page}/{totalPages}'
|
|
|
|
// possible variables: {page}, {totalPages}, {filteredPages}, {startRow}, {endRow}, {filteredRows} and {totalRows}
|
|
|
|
output: '{startRow} to {endRow} of {totalRows} rows', // '{page}/{totalPages}'
|
|
|
|
|
|
|
|
// apply disabled classname to the pager arrows when the rows at either extreme is visible
|
|
|
|
updateArrows: true,
|
|
|
|
|
|
|
|
// starting page of the pager (zero based index)
|
|
|
|
page: 0,
|
|
|
|
|
|
|
|
// Number of visible rows
|
|
|
|
size: 10,
|
|
|
|
|
|
|
|
// 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: false,
|
|
|
|
|
|
|
|
// 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, // removing rows in larger tables speeds up the sort
|
|
|
|
|
|
|
|
// css class names of pager arrows
|
|
|
|
cssFirst: '.first', // go to first page arrow
|
|
|
|
cssPrev: '.prev', // previous page arrow
|
|
|
|
cssNext: '.next', // next page arrow
|
|
|
|
cssLast: '.last', // go to last page arrow
|
|
|
|
cssGoto: '.gotoPage', // go to page selector - select dropdown that sets the current page
|
|
|
|
cssPageDisplay: '.pagedisplay', // location of where the "output" is displayed
|
|
|
|
cssPageSize: '.pagesize', // page size selector - select dropdown that sets the "size" option
|
2012-12-18 21:50:42 +00:00
|
|
|
cssErrorRow: 'tablesorter-errorRow', // error information row
|
2012-11-28 02:55:36 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// stuff not set by the user
|
|
|
|
totalRows: 0,
|
|
|
|
totalPages: 0,
|
|
|
|
filteredRows: 0,
|
2012-12-18 21:50:42 +00:00
|
|
|
filteredPages: 0
|
2012-11-28 02:55:36 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
var $this = this,
|
|
|
|
|
|
|
|
// hide arrows at extremes
|
|
|
|
pagerArrows = function(c, disable) {
|
|
|
|
var a = 'addClass',
|
|
|
|
r = 'removeClass',
|
|
|
|
d = c.cssDisabled,
|
|
|
|
dis = !!disable,
|
|
|
|
tp = Math.min( c.totalPages, c.filteredPages );
|
|
|
|
if ( c.updateArrows ) {
|
2013-05-06 14:09:22 +00:00
|
|
|
c.$container.find(c.cssFirst + ',' + c.cssPrev)[ ( dis || c.page === 0 ) ? a : r ](d);
|
|
|
|
c.$container.find(c.cssNext + ',' + c.cssLast)[ ( dis || c.page === tp - 1 ) ? a : r ](d);
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-06 14:16:38 +00:00
|
|
|
updatePageDisplay = function(table, c, flag) {
|
2013-04-26 16:22:47 +00:00
|
|
|
var i, p, s, t, out,
|
2013-05-06 14:09:22 +00:00
|
|
|
tc = table.config,
|
2013-04-26 16:22:47 +00:00
|
|
|
f = $(table).hasClass('hasFilters') && !c.ajaxUrl;
|
2013-05-06 14:09:22 +00:00
|
|
|
c.totalPages = Math.ceil( c.totalRows / c.size ); // needed for "pageSize" method
|
2013-05-07 15:13:50 +00:00
|
|
|
c.filteredRows = (f) ? tc.$tbodies.eq(0).children('tr:not(.' + (tc.widgetOptions && tc.widgetOptions.filter_filteredRow || 'filtered') + ',' + tc.selectorRemove + ')').length : c.totalRows;
|
2013-05-07 15:17:00 +00:00
|
|
|
c.filteredPages = (f) ? Math.ceil( c.filteredRows / c.size ) || 1 : c.totalPages;
|
|
|
|
if ( Math.min( c.totalPages, c.filteredPages ) >= 0 ) {
|
2012-11-28 02:55:36 +00:00
|
|
|
t = (c.size * c.page > c.filteredRows);
|
2013-05-07 15:17:00 +00:00
|
|
|
c.startRow = (t) ? 1 : (c.filteredRows === 0 ? 0 : c.size * c.page + 1);
|
2012-11-28 02:55:36 +00:00
|
|
|
c.page = (t) ? 0 : c.page;
|
|
|
|
c.endRow = Math.min( c.filteredRows, c.totalRows, c.size * ( c.page + 1 ) );
|
2013-05-06 14:09:22 +00:00
|
|
|
out = c.$container.find(c.cssPageDisplay);
|
2012-11-28 02:55:36 +00:00
|
|
|
// form the output string
|
|
|
|
s = c.output.replace(/\{(page|filteredRows|filteredPages|totalPages|startRow|endRow|totalRows)\}/gi, function(m){
|
|
|
|
return {
|
|
|
|
'{page}' : c.page + 1,
|
|
|
|
'{filteredRows}' : c.filteredRows,
|
|
|
|
'{filteredPages}' : c.filteredPages,
|
|
|
|
'{totalPages}' : c.totalPages,
|
|
|
|
'{startRow}' : c.startRow,
|
|
|
|
'{endRow}' : c.endRow,
|
|
|
|
'{totalRows}' : c.totalRows
|
|
|
|
}[m];
|
|
|
|
});
|
2013-05-06 14:09:22 +00:00
|
|
|
if (out.length) {
|
2012-11-28 02:55:36 +00:00
|
|
|
out[ (out[0].tagName === 'INPUT') ? 'val' : 'html' ](s);
|
2013-05-06 14:09:22 +00:00
|
|
|
if ( c.$goto.length ) {
|
2012-11-28 02:55:36 +00:00
|
|
|
t = '';
|
|
|
|
p = Math.min( c.totalPages, c.filteredPages );
|
|
|
|
for ( i = 1; i <= p; i++ ) {
|
|
|
|
t += '<option>' + i + '</option>';
|
|
|
|
}
|
2013-05-06 14:16:38 +00:00
|
|
|
c.$goto.html(t).val( c.page + 1 );
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pagerArrows(c);
|
2013-05-06 14:16:38 +00:00
|
|
|
if (c.initialized && flag !== false) { $(table).trigger('pagerComplete', c); }
|
2012-11-28 02:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fixHeight = function(table, c) {
|
2013-05-07 15:13:50 +00:00
|
|
|
var d, h, $b = table.config.$tbodies.eq(0);
|
2012-11-28 02:55:36 +00:00
|
|
|
if (c.fixedHeight) {
|
|
|
|
$b.find('tr.pagerSavedHeightSpacer').remove();
|
|
|
|
h = $.data(table, 'pagerSavedHeight');
|
|
|
|
if (h) {
|
|
|
|
d = h - $b.height();
|
2012-12-19 05:02:04 +00:00
|
|
|
if ( d > 5 && $.data(table, 'pagerLastSize') === c.size && $b.children('tr:visible').length < c.size ) {
|
2013-01-29 22:42:23 +00:00
|
|
|
$b.append('<tr class="pagerSavedHeightSpacer ' + table.config.selectorRemove.replace(/(tr)?\./g,'') + '" style="height:' + d + 'px;"></tr>');
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
changeHeight = function(table, c) {
|
2013-05-07 15:13:50 +00:00
|
|
|
var $b = table.config.$tbodies.eq(0);
|
2012-11-28 02:55:36 +00:00
|
|
|
$b.find('tr.pagerSavedHeightSpacer').remove();
|
|
|
|
$.data(table, 'pagerSavedHeight', $b.height());
|
|
|
|
fixHeight(table, c);
|
|
|
|
$.data(table, 'pagerLastSize', c.size);
|
|
|
|
},
|
|
|
|
|
|
|
|
hideRows = function(table, c){
|
|
|
|
if (!c.ajaxUrl) {
|
|
|
|
var i,
|
2013-05-06 14:16:38 +00:00
|
|
|
tc = table.config,
|
2013-05-07 15:13:50 +00:00
|
|
|
rows = tc.$tbodies.eq(0).children('tr:not(.' + tc.cssChildRow + ')'),
|
2012-11-28 02:55:36 +00:00
|
|
|
l = rows.length,
|
|
|
|
s = ( c.page * c.size ),
|
|
|
|
e = s + c.size,
|
2013-04-26 16:22:47 +00:00
|
|
|
f = tc.widgetOptions && tc.widgetOptions.filter_filteredRow || 'filtered',
|
2012-11-28 02:55:36 +00:00
|
|
|
j = 0; // size counter
|
|
|
|
for ( i = 0; i < l; i++ ){
|
2013-04-26 16:22:47 +00:00
|
|
|
if ( !rows[i].className.match(f) ) {
|
2012-11-28 02:55:36 +00:00
|
|
|
rows[i].style.display = ( j >= s && j < e ) ? '' : 'none';
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hideRowsSetup = function(table, c){
|
2013-05-06 14:09:22 +00:00
|
|
|
c.size = parseInt( c.$size.val(), 10 ) || c.size;
|
2012-11-28 02:55:36 +00:00
|
|
|
$.data(table, 'pagerLastSize', c.size);
|
|
|
|
pagerArrows(c);
|
|
|
|
if ( !c.removeRows ) {
|
|
|
|
hideRows(table, c);
|
|
|
|
$(table).bind('sortEnd.pager filterEnd.pager', function(){
|
|
|
|
hideRows(table, c);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-05-07 15:13:50 +00:00
|
|
|
renderAjax = function(data, table, c, xhr, exception){
|
2012-11-28 02:55:36 +00:00
|
|
|
// process data
|
|
|
|
if ( typeof(c.ajaxProcessing) === "function" ) {
|
|
|
|
// ajaxProcessing result: [ total, rows, headers ]
|
2013-05-07 15:13:50 +00:00
|
|
|
var i, j, hsh, $f, $sh, th, d, l, $err,
|
2012-11-28 02:55:36 +00:00
|
|
|
$t = $(table),
|
|
|
|
tc = table.config,
|
|
|
|
hl = $t.find('thead th').length, tds = '',
|
2013-05-07 15:13:50 +00:00
|
|
|
result = c.ajaxProcessing(data, table) || [ 0, [] ],
|
2013-04-12 18:02:08 +00:00
|
|
|
// allow [ total, rows, headers ] or [ rows, total, headers ]
|
|
|
|
t = isNaN(result[0]) && !isNaN(result[1]);
|
2013-03-28 01:01:39 +00:00
|
|
|
|
2012-12-18 21:50:42 +00:00
|
|
|
$t.find('thead tr.' + c.cssErrorRow).remove(); // Clean up any previous error.
|
2013-05-07 15:13:50 +00:00
|
|
|
|
2012-11-28 02:55:36 +00:00
|
|
|
if ( exception ) {
|
2013-05-07 15:13:50 +00:00
|
|
|
$err = $('<tr class="' + c.cssErrorRow + '"><td style="text-align:center;" colspan="' + hl + '">' + (
|
|
|
|
xhr.status === 0 ? 'Not connected, verify Network' :
|
|
|
|
xhr.status === 404 ? 'Requested page not found [404]' :
|
|
|
|
xhr.status === 500 ? 'Internal Server Error [500]' :
|
|
|
|
exception === 'parsererror' ? 'Requested JSON parse failed' :
|
|
|
|
exception === 'timeout' ? 'Time out error' :
|
|
|
|
exception === 'abort' ? 'Ajax Request aborted' :
|
|
|
|
'Uncaught error: ' + xhr.statusText + ' [' + xhr.status + ']' ) + '</td></tr>')
|
|
|
|
.click(function(){
|
|
|
|
$(this).remove();
|
|
|
|
})
|
2012-11-28 02:55:36 +00:00
|
|
|
// add error row to thead instead of tbody, or clicking on the header will result in a parser error
|
2013-05-07 15:13:50 +00:00
|
|
|
.appendTo( $t.find('thead:first') );
|
|
|
|
tc.$tbodies.eq(0).empty();
|
2012-11-28 02:55:36 +00:00
|
|
|
} else {
|
2013-05-07 15:13:50 +00:00
|
|
|
c.totalRows = result[t ? 1 : 0] || c.totalRows || 0;
|
|
|
|
d = result[t ? 0 : 1] || []; // row data
|
|
|
|
l = d.length;
|
|
|
|
th = result[2]; // headers
|
|
|
|
if (d instanceof jQuery) {
|
|
|
|
// append jQuery object
|
|
|
|
tc.$tbodies.eq(0).empty().append(d);
|
|
|
|
} else if (d.length) {
|
|
|
|
// build table from array
|
|
|
|
if ( l > 0 ) {
|
|
|
|
for ( i = 0; i < l; i++ ) {
|
|
|
|
tds += '<tr>';
|
|
|
|
for ( j = 0; j < d[i].length; j++ ) {
|
|
|
|
// build tbody cells
|
|
|
|
tds += '<td>' + d[i][j] + '</td>';
|
|
|
|
}
|
|
|
|
tds += '</tr>';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add rows to first tbody
|
|
|
|
tc.$tbodies.eq(0).html( tds );
|
|
|
|
}
|
|
|
|
// only add new header text if the length matches
|
|
|
|
if ( th && th.length === hl ) {
|
|
|
|
hsh = $t.hasClass('hasStickyHeaders');
|
|
|
|
$sh = hsh ? tc.$sticky.children('thead:first').children().children() : '';
|
|
|
|
$f = $t.find('tfoot tr:first').children();
|
|
|
|
$t.find('th.' + tc.cssHeader).each(function(j){
|
|
|
|
var $t = $(this), icn;
|
|
|
|
// add new test within the first span it finds, or just in the header
|
|
|
|
if ( $t.find('.' + tc.cssIcon).length ) {
|
|
|
|
icn = $t.find('.' + tc.cssIcon).clone(true);
|
|
|
|
$t.find('.tablesorter-header-inner').html( th[j] ).append(icn);
|
|
|
|
if ( hsh && $sh.length ) {
|
|
|
|
icn = $sh.eq(j).find('.' + tc.cssIcon).clone(true);
|
|
|
|
$sh.eq(j).find('.tablesorter-header-inner').html( th[j] ).append(icn);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$t.find('.tablesorter-header-inner').html( th[j] );
|
|
|
|
if (hsh && $sh.length) {
|
|
|
|
$sh.eq(j).find('.tablesorter-header-inner').html( th[j] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$f.eq(j).html( th[j] );
|
|
|
|
});
|
|
|
|
}
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
2012-12-17 17:33:46 +00:00
|
|
|
if (tc.showProcessing) {
|
|
|
|
$.tablesorter.isProcessing(table); // remove loading icon
|
|
|
|
}
|
2012-11-28 02:55:36 +00:00
|
|
|
$t.trigger('update');
|
|
|
|
c.totalPages = Math.ceil( c.totalRows / c.size );
|
|
|
|
updatePageDisplay(table, c);
|
|
|
|
fixHeight(table, c);
|
|
|
|
if (c.initialized) { $t.trigger('pagerChange', c); }
|
|
|
|
}
|
|
|
|
if (!c.initialized) {
|
|
|
|
c.initialized = true;
|
|
|
|
$(table).trigger('pagerInitialized', c);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getAjax = function(table, c){
|
2012-12-17 17:33:46 +00:00
|
|
|
var url = getAjaxUrl(table, c),
|
2013-05-07 15:13:50 +00:00
|
|
|
$doc = $(document),
|
2012-12-17 17:33:46 +00:00
|
|
|
tc = table.config;
|
|
|
|
if ( url !== '' ) {
|
|
|
|
if (tc.showProcessing) {
|
|
|
|
$.tablesorter.isProcessing(table, true); // show loading icon
|
|
|
|
}
|
2013-05-07 15:13:50 +00:00
|
|
|
$doc.bind('ajaxError.pager', function(e, xhr, settings, exception) {
|
|
|
|
if (url.match(settings.url)) {
|
|
|
|
renderAjax(null, table, c, xhr, exception);
|
|
|
|
$doc.unbind('ajaxError.pager');
|
2012-12-17 17:33:46 +00:00
|
|
|
}
|
|
|
|
});
|
2013-05-08 22:02:36 +00:00
|
|
|
c.ajaxObject.url = url; // from the ajaxUrl option and modified by customAjaxUrl
|
|
|
|
c.ajaxObject.success = function(data) {
|
2012-12-17 17:33:46 +00:00
|
|
|
renderAjax(data, table, c);
|
2013-05-07 15:13:50 +00:00
|
|
|
$doc.unbind('ajaxError.pager');
|
2013-05-08 22:02:36 +00:00
|
|
|
if (typeof c.oldAjaxSuccess === 'function') {
|
|
|
|
c.oldAjaxSuccess(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
$.ajax(c.ajaxObject);
|
2012-12-17 17:33:46 +00:00
|
|
|
}
|
|
|
|
},
|
2013-03-28 01:01:39 +00:00
|
|
|
|
2012-12-17 17:33:46 +00:00
|
|
|
getAjaxUrl = function(table, c) {
|
2013-04-04 21:32:03 +00:00
|
|
|
var url = (c.ajaxUrl) ? c.ajaxUrl
|
|
|
|
// allow using "{page+1}" in the url string to switch to a non-zero based index
|
|
|
|
.replace(/\{page([\-+]\d+)?\}/, function(s,n){ return c.page + (n ? parseInt(n, 10) : 0); })
|
|
|
|
.replace(/\{size\}/g, c.size) : '',
|
2012-11-28 02:55:36 +00:00
|
|
|
sl = table.config.sortList,
|
2012-12-17 17:33:46 +00:00
|
|
|
fl = c.currentFilters || [],
|
2013-04-12 18:02:08 +00:00
|
|
|
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
|
|
|
|
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
|
2012-12-17 17:33:46 +00:00
|
|
|
arry = [];
|
|
|
|
if (sortCol) {
|
|
|
|
sortCol = sortCol[1];
|
2012-11-28 02:55:36 +00:00
|
|
|
$.each(sl, function(i,v){
|
2012-12-17 17:33:46 +00:00
|
|
|
arry.push(sortCol + '[' + v[0] + ']=' + v[1]);
|
2012-11-28 02:55:36 +00:00
|
|
|
});
|
|
|
|
// if the arry is empty, just add the col parameter... "&{sortList:col}" becomes "&col"
|
2013-04-12 18:02:08 +00:00
|
|
|
url = url.replace(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : sortCol );
|
|
|
|
arry = [];
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
2012-12-17 17:33:46 +00:00
|
|
|
if (filterCol) {
|
|
|
|
filterCol = filterCol[1];
|
|
|
|
$.each(fl, function(i,v){
|
|
|
|
if (v) {
|
|
|
|
arry.push(filterCol + '[' + i + ']=' + encodeURIComponent(v));
|
|
|
|
}
|
2012-11-28 02:55:36 +00:00
|
|
|
});
|
2012-12-17 17:33:46 +00:00
|
|
|
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
|
2013-04-12 18:02:08 +00:00
|
|
|
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
2013-03-28 01:01:39 +00:00
|
|
|
if ( typeof(c.customAjaxUrl) === "function" ) {
|
|
|
|
url = c.customAjaxUrl(table, url);
|
|
|
|
}
|
2012-12-17 17:33:46 +00:00
|
|
|
return url;
|
2012-11-28 02:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
renderTable = function(table, rows, c) {
|
2013-01-04 18:10:17 +00:00
|
|
|
c.isDisabled = false; // needed because sorting will change the page and re-enable the pager
|
2013-05-07 15:13:50 +00:00
|
|
|
var i, j, o, $tb,
|
2012-11-28 02:55:36 +00:00
|
|
|
l = rows.length,
|
|
|
|
s = ( c.page * c.size ),
|
|
|
|
e = ( s + c.size );
|
|
|
|
if ( l < 1 ) { return; } // empty table, abort!
|
|
|
|
if (c.initialized) { $(table).trigger('pagerChange', c); }
|
|
|
|
if ( !c.removeRows ) {
|
|
|
|
hideRows(table, c);
|
|
|
|
} else {
|
|
|
|
if ( e > rows.length ) {
|
|
|
|
e = rows.length;
|
|
|
|
}
|
|
|
|
$.tablesorter.clearTableBody(table);
|
2013-05-07 15:13:50 +00:00
|
|
|
$tb = $.tablesorter.processTbody(table, table.config.$tbodies.eq(0), true);
|
2012-11-28 02:55:36 +00:00
|
|
|
for ( i = s; i < e; i++ ) {
|
|
|
|
o = rows[i];
|
|
|
|
l = o.length;
|
|
|
|
for ( j = 0; j < l; j++ ) {
|
2013-05-07 15:13:50 +00:00
|
|
|
$tb.appendChild(o[j]);
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-07 15:13:50 +00:00
|
|
|
$.tablesorter.processTbody(table, $tb, false);
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
if ( c.page >= c.totalPages ) {
|
|
|
|
moveToLastPage(table, c);
|
|
|
|
}
|
|
|
|
updatePageDisplay(table, c);
|
|
|
|
if ( !c.isDisabled ) { fixHeight(table, c); }
|
|
|
|
$(table).trigger('applyWidgets');
|
|
|
|
},
|
|
|
|
|
|
|
|
showAllRows = function(table, c){
|
|
|
|
if ( c.ajax ) {
|
|
|
|
pagerArrows(c, true);
|
|
|
|
} else {
|
|
|
|
c.isDisabled = true;
|
|
|
|
$.data(table, 'pagerLastPage', c.page);
|
|
|
|
$.data(table, 'pagerLastSize', c.size);
|
|
|
|
c.page = 0;
|
|
|
|
c.size = c.totalRows;
|
|
|
|
c.totalPages = 1;
|
2013-05-07 15:13:50 +00:00
|
|
|
$(table).find('tr.pagerSavedHeightSpacer').remove();
|
2012-11-28 02:55:36 +00:00
|
|
|
renderTable(table, table.config.rowsCopy, c);
|
|
|
|
}
|
|
|
|
// disable size selector
|
2013-05-06 14:09:22 +00:00
|
|
|
c.$size.add(c.$goto).each(function(){
|
2012-11-28 02:55:36 +00:00
|
|
|
$(this).addClass(c.cssDisabled)[0].disabled = true;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-04-12 18:02:08 +00:00
|
|
|
moveToPage = function(table, c, flag) {
|
2012-11-28 02:55:36 +00:00
|
|
|
if ( c.isDisabled ) { return; }
|
|
|
|
var p = Math.min( c.totalPages, c.filteredPages );
|
2013-02-10 17:47:59 +00:00
|
|
|
if ( c.page < 0 ) { c.page = 0; }
|
2013-02-20 07:17:27 +00:00
|
|
|
if ( c.page > ( p - 1 ) && p !== 0 ) { c.page = p - 1; }
|
2012-12-17 17:33:46 +00:00
|
|
|
if (c.ajax) {
|
2012-11-28 02:55:36 +00:00
|
|
|
getAjax(table, c);
|
|
|
|
} else if (!c.ajax) {
|
|
|
|
renderTable(table, table.config.rowsCopy, c);
|
|
|
|
}
|
|
|
|
$.data(table, 'pagerLastPage', c.page);
|
2012-12-17 17:33:46 +00:00
|
|
|
$.data(table, 'pagerUpdateTriggered', true);
|
2013-04-12 18:02:08 +00:00
|
|
|
if (c.initialized && flag !== false) {
|
|
|
|
$(table).trigger('pageMoved', c);
|
|
|
|
}
|
2012-11-28 02:55:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setPageSize = function(table, size, c) {
|
|
|
|
c.size = size;
|
|
|
|
$.data(table, 'pagerLastPage', c.page);
|
|
|
|
$.data(table, 'pagerLastSize', c.size);
|
|
|
|
c.totalPages = Math.ceil( c.totalRows / c.size );
|
|
|
|
moveToPage(table, c);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveToFirstPage = function(table, c) {
|
|
|
|
c.page = 0;
|
|
|
|
moveToPage(table, c);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveToLastPage = function(table, c) {
|
|
|
|
c.page = ( Math.min( c.totalPages, c.filteredPages ) - 1 );
|
|
|
|
moveToPage(table, c);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveToNextPage = function(table, c) {
|
|
|
|
c.page++;
|
|
|
|
if ( c.page >= ( Math.min( c.totalPages, c.filteredPages ) - 1 ) ) {
|
|
|
|
c.page = ( Math.min( c.totalPages, c.filteredPages ) - 1 );
|
|
|
|
}
|
|
|
|
moveToPage(table, c);
|
|
|
|
},
|
|
|
|
|
|
|
|
moveToPrevPage = function(table, c) {
|
|
|
|
c.page--;
|
|
|
|
if ( c.page <= 0 ) {
|
|
|
|
c.page = 0;
|
|
|
|
}
|
|
|
|
moveToPage(table, c);
|
|
|
|
},
|
|
|
|
|
|
|
|
destroyPager = function(table, c){
|
|
|
|
showAllRows(table, c);
|
2013-05-06 14:09:22 +00:00
|
|
|
c.$container.hide(); // hide pager
|
2012-11-28 02:55:36 +00:00
|
|
|
table.config.appender = null; // remove pager appender function
|
|
|
|
$(table).unbind('destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager');
|
|
|
|
},
|
|
|
|
|
|
|
|
enablePager = function(table, c, triggered){
|
2013-05-06 14:09:22 +00:00
|
|
|
var p = c.$size.removeClass(c.cssDisabled).removeAttr('disabled');
|
|
|
|
c.$goto.removeClass(c.cssDisabled).removeAttr('disabled');
|
2012-11-28 02:55:36 +00:00
|
|
|
c.isDisabled = false;
|
|
|
|
c.page = $.data(table, 'pagerLastPage') || c.page || 0;
|
|
|
|
c.size = $.data(table, 'pagerLastSize') || parseInt(p.find('option[selected]').val(), 10) || c.size;
|
|
|
|
p.val(c.size); // set page size
|
|
|
|
c.totalPages = Math.ceil( Math.min( c.totalPages, c.filteredPages ) / c.size);
|
|
|
|
if ( triggered ) {
|
|
|
|
$(table).trigger('update');
|
|
|
|
setPageSize(table, c.size, c);
|
|
|
|
hideRowsSetup(table, c);
|
|
|
|
fixHeight(table, c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$this.appender = function(table, rows) {
|
|
|
|
var c = table.config.pager;
|
|
|
|
if ( !c.ajax ) {
|
|
|
|
table.config.rowsCopy = rows;
|
|
|
|
c.totalRows = rows.length;
|
|
|
|
c.size = $.data(table, 'pagerLastSize') || c.size;
|
|
|
|
c.totalPages = Math.ceil(c.totalRows / c.size);
|
|
|
|
renderTable(table, rows, c);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$this.construct = function(settings) {
|
|
|
|
return this.each(function() {
|
|
|
|
// check if tablesorter has initialized
|
|
|
|
if (!(this.config && this.hasInitialized)) { return; }
|
2012-12-19 16:35:46 +00:00
|
|
|
var t, ctrls, fxn,
|
|
|
|
config = this.config,
|
2012-11-28 02:55:36 +00:00
|
|
|
c = config.pager = $.extend( {}, $.tablesorterPager.defaults, settings ),
|
|
|
|
table = this,
|
2012-12-17 17:33:46 +00:00
|
|
|
tc = table.config,
|
2012-11-28 02:55:36 +00:00
|
|
|
$t = $(table),
|
2013-05-08 22:02:36 +00:00
|
|
|
// added in case the pager is reinitialized after being destroyed.
|
|
|
|
pager = c.$container = $(c.container).addClass('tablesorter-pager').show();
|
|
|
|
c.oldAjaxSuccess = c.oldAjaxSuccess || c.ajaxObject.success;
|
2012-11-28 02:55:36 +00:00
|
|
|
config.appender = $this.appender;
|
2012-12-19 16:35:46 +00:00
|
|
|
|
|
|
|
$t
|
2013-01-22 16:34:30 +00:00
|
|
|
.unbind('filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager')
|
2012-12-17 17:33:46 +00:00
|
|
|
.bind('filterStart.pager', function(e, filters) {
|
2013-01-04 18:10:17 +00:00
|
|
|
$.data(table, 'pagerUpdateTriggered', false);
|
2012-12-17 17:33:46 +00:00
|
|
|
c.currentFilters = filters;
|
2012-12-19 16:35:46 +00:00
|
|
|
})
|
|
|
|
// update pager after filter widget completes
|
2012-12-26 07:46:49 +00:00
|
|
|
.bind('filterEnd.pager sortEnd.pager', function(e) {
|
2012-12-17 17:33:46 +00:00
|
|
|
//Prevent infinite event loops from occuring by setting this in all moveToPage calls and catching it here.
|
|
|
|
if ($.data(table, 'pagerUpdateTriggered')) {
|
|
|
|
$.data(table, 'pagerUpdateTriggered', false);
|
|
|
|
return;
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
2013-04-12 18:02:08 +00:00
|
|
|
moveToPage(table, c, false);
|
2013-05-06 14:16:38 +00:00
|
|
|
updatePageDisplay(table, c, false);
|
2013-01-04 18:10:17 +00:00
|
|
|
fixHeight(table, c);
|
2012-12-19 16:35:46 +00:00
|
|
|
})
|
2013-03-26 21:16:13 +00:00
|
|
|
.bind('disable.pager', function(e){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2012-12-19 16:35:46 +00:00
|
|
|
showAllRows(table, c);
|
|
|
|
})
|
2013-03-26 21:16:13 +00:00
|
|
|
.bind('enable.pager', function(e){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2012-12-19 16:35:46 +00:00
|
|
|
enablePager(table, c, true);
|
|
|
|
})
|
2013-03-26 21:16:13 +00:00
|
|
|
.bind('destroy.pager', function(e){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2012-12-19 16:35:46 +00:00
|
|
|
destroyPager(table, c);
|
|
|
|
})
|
2013-03-26 21:16:13 +00:00
|
|
|
.bind('update.pager', function(e){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2012-12-19 16:35:46 +00:00
|
|
|
hideRows(table, c);
|
2013-01-22 16:34:30 +00:00
|
|
|
})
|
|
|
|
.bind('pageSize.pager', function(e,v){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2013-01-22 16:34:30 +00:00
|
|
|
c.size = parseInt(v, 10) || 10;
|
|
|
|
hideRows(table, c);
|
2013-05-06 14:16:38 +00:00
|
|
|
updatePageDisplay(table, c, false);
|
2013-05-06 14:09:22 +00:00
|
|
|
if (c.$size.length) { c.$size.val(c.size); } // twice?
|
2013-02-10 17:47:59 +00:00
|
|
|
})
|
|
|
|
.bind('pageSet.pager', function(e,v){
|
2013-03-23 14:41:33 +00:00
|
|
|
e.stopPropagation();
|
2013-02-10 17:47:59 +00:00
|
|
|
c.page = (parseInt(v, 10) || 1) - 1;
|
2013-05-06 14:09:22 +00:00
|
|
|
if (c.$goto.length) { c.$goto.val(c.size); } // twice?
|
2013-02-10 17:47:59 +00:00
|
|
|
moveToPage(table, c);
|
2013-05-06 14:16:38 +00:00
|
|
|
updatePageDisplay(table, c, false);
|
2012-11-28 02:55:36 +00:00
|
|
|
});
|
|
|
|
|
2012-12-19 16:35:46 +00:00
|
|
|
// clicked controls
|
2013-05-06 14:09:22 +00:00
|
|
|
ctrls = [ c.cssFirst, c.cssPrev, c.cssNext, c.cssLast ];
|
2012-12-19 16:35:46 +00:00
|
|
|
fxn = [ moveToFirstPage, moveToPrevPage, moveToNextPage, moveToLastPage ];
|
|
|
|
pager.find(ctrls.join(','))
|
|
|
|
.unbind('click.pager')
|
|
|
|
.bind('click.pager', function(e){
|
2013-05-06 14:09:22 +00:00
|
|
|
var i, $t = $(this), l = ctrls.length;
|
|
|
|
if ( !$t.hasClass(c.cssDisabled) ) {
|
2012-12-19 16:35:46 +00:00
|
|
|
for (i = 0; i < l; i++) {
|
2013-05-06 14:09:22 +00:00
|
|
|
if ($t.is(ctrls[i])) {
|
2012-12-19 16:35:46 +00:00
|
|
|
fxn[i](table, c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-11-28 02:55:36 +00:00
|
|
|
});
|
2012-12-19 16:35:46 +00:00
|
|
|
|
|
|
|
// goto selector
|
2013-05-06 14:09:22 +00:00
|
|
|
c.$goto = pager.find(c.cssGoto);
|
|
|
|
if ( c.$goto.length ) {
|
|
|
|
c.$goto
|
2012-12-19 16:35:46 +00:00
|
|
|
.unbind('change')
|
|
|
|
.bind('change', function(){
|
|
|
|
c.page = $(this).val() - 1;
|
|
|
|
moveToPage(table, c);
|
|
|
|
});
|
2013-05-06 14:16:38 +00:00
|
|
|
updatePageDisplay(table, c, false);
|
2012-11-28 02:55:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 16:35:46 +00:00
|
|
|
// page size selector
|
2013-05-06 14:09:22 +00:00
|
|
|
c.$size = pager.find(c.cssPageSize);
|
|
|
|
if ( c.$size.length ) {
|
|
|
|
c.$size.unbind('change.pager').bind('change.pager', function() {
|
|
|
|
c.$size.val( $(this).val() ); // in case there are more than one pagers
|
2012-12-19 16:35:46 +00:00
|
|
|
if ( !$(this).hasClass(c.cssDisabled) ) {
|
|
|
|
setPageSize(table, parseInt( $(this).val(), 10 ), c);
|
|
|
|
changeHeight(table, c);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear initialized flag
|
|
|
|
c.initialized = false;
|
|
|
|
// before initialization event
|
|
|
|
$t.trigger('pagerBeforeInitialized', c);
|
|
|
|
|
|
|
|
enablePager(table, c, false);
|
|
|
|
|
|
|
|
if ( typeof(c.ajaxUrl) === 'string' ) {
|
|
|
|
// ajax pager; interact with database
|
|
|
|
c.ajax = true;
|
|
|
|
//When filtering with ajax, allow only custom filtering function, disable default filtering since it will be done server side.
|
|
|
|
tc.widgetOptions.filter_serversideFiltering = true;
|
|
|
|
tc.serverSideSorting = true;
|
2012-12-20 19:36:22 +00:00
|
|
|
moveToPage(table, c);
|
2012-12-19 16:35:46 +00:00
|
|
|
} else {
|
|
|
|
c.ajax = false;
|
|
|
|
// Regular pager; all rows stored in memory
|
|
|
|
$(this).trigger("appendCache", true);
|
|
|
|
hideRowsSetup(table, c);
|
|
|
|
}
|
2012-11-28 02:55:36 +00:00
|
|
|
|
2013-01-04 18:10:17 +00:00
|
|
|
changeHeight(table, c);
|
2013-03-28 01:01:39 +00:00
|
|
|
|
2012-11-28 02:55:36 +00:00
|
|
|
// pager initialized
|
|
|
|
if (!c.ajax) {
|
|
|
|
c.initialized = true;
|
|
|
|
$(table).trigger('pagerInitialized', c);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
}()
|
|
|
|
});
|
|
|
|
// extend plugin scope
|
|
|
|
$.fn.extend({
|
|
|
|
tablesorterPager: $.tablesorterPager.construct
|
|
|
|
});
|
|
|
|
|
2013-04-12 18:02:08 +00:00
|
|
|
})(jQuery);
|