merged in pull #336

This commit is contained in:
Mottie 2013-09-30 09:23:14 -05:00
parent b561ff4471
commit 214bce4e95

View File

@ -88,90 +88,92 @@
var $this = this, var $this = this,
// hide arrows at extremes // hide arrows at extremes
pagerArrows = function(c, disable) { pagerArrows = function(p, disable) {
var a = 'addClass', var a = 'addClass',
r = 'removeClass', r = 'removeClass',
d = c.cssDisabled, d = p.cssDisabled,
dis = !!disable, dis = !!disable,
tp = Math.min( c.totalPages, c.filteredPages ); tp = Math.min( p.totalPages, p.filteredPages );
if ( c.updateArrows ) { if ( p.updateArrows ) {
c.$container.find(c.cssFirst + ',' + c.cssPrev)[ ( dis || c.page === 0 ) ? a : r ](d); p.$container.find(p.cssFirst + ',' + p.cssPrev)[ ( dis || p.page === 0 ) ? a : r ](d);
c.$container.find(c.cssNext + ',' + c.cssLast)[ ( dis || c.page === tp - 1 ) ? a : r ](d); p.$container.find(p.cssNext + ',' + p.cssLast)[ ( dis || p.page === tp - 1 ) ? a : r ](d);
} }
}, },
updatePageDisplay = function(table, c, flag) { updatePageDisplay = function(table, p, flag) {
var i, p, s, t, out, var i, pg, s, t, out,
tc = table.config, c = table.config,
f = $(table).hasClass('hasFilters') && !c.ajaxUrl; f = c.$table.hasClass('hasFilters') && !p.ajaxUrl;
c.totalPages = Math.ceil( c.totalRows / c.size ); // needed for "pageSize" method p.totalPages = Math.ceil( p.totalRows / p.size ); // needed for "pageSize" method
c.filteredRows = (f) ? tc.$tbodies.eq(0).children('tr:not(.' + (tc.widgetOptions && tc.widgetOptions.filter_filteredRow || 'filtered') + ',' + tc.selectorRemove + ')').length : c.totalRows; p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove + ')').length : p.totalRows;
c.filteredPages = (f) ? Math.ceil( c.filteredRows / c.size ) || 1 : c.totalPages; p.filteredPages = (f) ? Math.ceil( p.filteredRows / p.size ) || 1 : p.totalPages;
if ( Math.min( c.totalPages, c.filteredPages ) >= 0 ) { if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) {
t = (c.size * c.page > c.filteredRows); t = (p.size * p.page > p.filteredRows);
c.startRow = (t) ? 1 : (c.filteredRows === 0 ? 0 : c.size * c.page + 1); p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1);
c.page = (t) ? 0 : c.page; p.page = (t) ? 0 : p.page;
c.endRow = Math.min( c.filteredRows, c.totalRows, c.size * ( c.page + 1 ) ); p.endRow = Math.min( p.filteredRows, p.totalRows, p.size * ( p.page + 1 ) );
out = c.$container.find(c.cssPageDisplay); out = p.$container.find(p.cssPageDisplay);
// form the output string // form the output string
s = c.output.replace(/\{(page|filteredRows|filteredPages|totalPages|startRow|endRow|totalRows)\}/gi, function(m){ s = p.output.replace(/\{(page|filteredRows|filteredPages|totalPages|startRow|endRow|totalRows)\}/gi, function(m){
return { return {
'{page}' : c.page + 1, '{page}' : p.page + 1,
'{filteredRows}' : c.filteredRows, '{filteredRows}' : p.filteredRows,
'{filteredPages}' : c.filteredPages, '{filteredPages}' : p.filteredPages,
'{totalPages}' : c.totalPages, '{totalPages}' : p.totalPages,
'{startRow}' : c.startRow, '{startRow}' : p.startRow,
'{endRow}' : c.endRow, '{endRow}' : p.endRow,
'{totalRows}' : c.totalRows '{totalRows}' : p.totalRows
}[m]; }[m];
}); });
if (out.length) { if (out.length) {
out[ (out[0].tagName === 'INPUT') ? 'val' : 'html' ](s); out[ (out[0].tagName === 'INPUT') ? 'val' : 'html' ](s);
if ( c.$goto.length ) { if ( p.$goto.length ) {
t = ''; t = '';
p = Math.min( c.totalPages, c.filteredPages ); pg = Math.min( p.totalPages, p.filteredPages );
for ( i = 1; i <= p; i++ ) { for ( i = 1; i <= pg; i++ ) {
t += '<option>' + i + '</option>'; t += '<option>' + i + '</option>';
} }
c.$goto.html(t).val( c.page + 1 ); p.$goto.html(t).val( p.page + 1 );
} }
} }
} }
pagerArrows(c); pagerArrows(p);
if (c.initialized && flag !== false) { $(table).trigger('pagerComplete', c); } if (p.initialized && flag !== false) { c.$table.trigger('pagerComplete', p); }
}, },
fixHeight = function(table, c) { fixHeight = function(table, p) {
var d, h, $b = table.config.$tbodies.eq(0); var d, h,
if (c.fixedHeight) { c = table.config,
$b = c.$tbodies.eq(0);
if (p.fixedHeight) {
$b.find('tr.pagerSavedHeightSpacer').remove(); $b.find('tr.pagerSavedHeightSpacer').remove();
h = $.data(table, 'pagerSavedHeight'); h = $.data(table, 'pagerSavedHeight');
if (h) { if (h) {
d = h - $b.height(); d = h - $b.height();
if ( d > 5 && $.data(table, 'pagerLastSize') === c.size && $b.children('tr:visible').length < c.size ) { if ( d > 5 && $.data(table, 'pagerLastSize') === p.size && $b.children('tr:visible').length < p.size ) {
$b.append('<tr class="pagerSavedHeightSpacer ' + table.config.selectorRemove.replace(/(tr)?\./g,'') + '" style="height:' + d + 'px;"></tr>'); $b.append('<tr class="pagerSavedHeightSpacer ' + c.selectorRemove.replace(/(tr)?\./g,'') + '" style="height:' + d + 'px;"></tr>');
} }
} }
} }
}, },
changeHeight = function(table, c) { changeHeight = function(table, p) {
var $b = table.config.$tbodies.eq(0); var $b = table.config.$tbodies.eq(0);
$b.find('tr.pagerSavedHeightSpacer').remove(); $b.find('tr.pagerSavedHeightSpacer').remove();
$.data(table, 'pagerSavedHeight', $b.height()); $.data(table, 'pagerSavedHeight', $b.height());
fixHeight(table, c); fixHeight(table, p);
$.data(table, 'pagerLastSize', c.size); $.data(table, 'pagerLastSize', p.size);
}, },
hideRows = function(table, c){ hideRows = function(table, p){
if (!c.ajaxUrl) { if (!p.ajaxUrl) {
var i, var i,
tc = table.config, c = table.config,
rows = tc.$tbodies.eq(0).children('tr:not(.' + tc.cssChildRow + ')'), rows = c.$tbodies.eq(0).children('tr:not(.' + c.cssChildRow + ')'),
l = rows.length, l = rows.length,
s = ( c.page * c.size ), s = ( p.page * p.size ),
e = s + c.size, e = s + p.size,
f = tc.widgetOptions && tc.widgetOptions.filter_filteredRow || 'filtered', f = c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered',
j = 0; // size counter j = 0; // size counter
for ( i = 0; i < l; i++ ){ for ( i = 0; i < l; i++ ){
if ( !rows[i].className.match(f) ) { if ( !rows[i].className.match(f) ) {
@ -182,34 +184,35 @@
} }
}, },
hideRowsSetup = function(table, c){ hideRowsSetup = function(table, p){
c.size = parseInt( c.$size.val(), 10 ) || c.size; p.size = parseInt( p.$size.val(), 10 ) || p.size;
$.data(table, 'pagerLastSize', c.size); $.data(table, 'pagerLastSize', p.size);
pagerArrows(c); pagerArrows(p);
if ( !c.removeRows ) { if ( !p.removeRows ) {
hideRows(table, c); hideRows(table, p);
$(table).bind('sortEnd.pager filterEnd.pager', function(){ $(table).bind('sortEnd.pager filterEnd.pager', function(){
hideRows(table, c); hideRows(table, p);
}); });
} }
}, },
renderAjax = function(data, table, c, xhr, exception){ renderAjax = function(data, table, p, xhr, exception){
// process data // process data
if ( typeof(c.ajaxProcessing) === "function" ) { if ( typeof(p.ajaxProcessing) === "function" ) {
// ajaxProcessing result: [ total, rows, headers ] // ajaxProcessing result: [ total, rows, headers ]
var i, j, hsh, $f, $sh, th, d, l, $err, rr_count, var i, j, hsh, $f, $sh, th, d, l, $err, rr_count,
$t = $(table), c = table.config,
tc = table.config, $t = c.$table,
result = c.ajaxProcessing(data, table) || [ 0, [] ], tds = '',
hl = $t.find('thead th').length, tds = '', result = p.ajaxProcessing(data, table) || [ 0, [] ],
hl = $t.find('thead th').length,
// allow [ total, rows, headers ] or [ rows, total, headers ] // allow [ total, rows, headers ] or [ rows, total, headers ]
t = isNaN(result[0]) && !isNaN(result[1]); t = isNaN(result[0]) && !isNaN(result[1]);
$t.find('thead tr.' + c.cssErrorRow).remove(); // Clean up any previous error. $t.find('thead tr.' + p.cssErrorRow).remove(); // Clean up any previous error.
if ( exception ) { if ( exception ) {
$err = $('<tr class="' + c.cssErrorRow + '"><td style="text-align:center;" colspan="' + hl + '">' + ( $err = $('<tr class="' + p.cssErrorRow + '"><td style="text-align:center;" colspan="' + hl + '">' + (
xhr.status === 0 ? 'Not connected, verify Network' : xhr.status === 0 ? 'Not connected, verify Network' :
xhr.status === 404 ? 'Requested page not found [404]' : xhr.status === 404 ? 'Requested page not found [404]' :
xhr.status === 500 ? 'Internal Server Error [500]' : xhr.status === 500 ? 'Internal Server Error [500]' :
@ -222,17 +225,17 @@
}) })
// add error row to thead instead of tbody, or clicking on the header will result in a parser error // add error row to thead instead of tbody, or clicking on the header will result in a parser error
.appendTo( $t.find('thead:first') ); .appendTo( $t.find('thead:first') );
tc.$tbodies.eq(0).empty(); c.$tbodies.eq(0).empty();
} else { } else {
//ensure a zero returned row count doesn't fail the logical || //ensure a zero returned row count doesn't fail the logical ||
rr_count = result[t ? 1 : 0]; rr_count = result[t ? 1 : 0];
c.totalRows = isNaN(rr_count) ? c.totalRows || 0 : rr_count; p.totalRows = isNaN(rr_count) ? p.totalRows || 0 : rr_count;
d = result[t ? 0 : 1] || []; // row data d = result[t ? 0 : 1] || []; // row data
l = d.length; l = d.length;
th = result[2]; // headers th = result[2]; // headers
if (d instanceof jQuery) { if (d instanceof jQuery) {
// append jQuery object // append jQuery object
tc.$tbodies.eq(0).empty().append(d); c.$tbodies.eq(0).empty().append(d);
} else if (d.length) { } else if (d.length) {
// build table from array // build table from array
if ( l > 0 ) { if ( l > 0 ) {
@ -246,21 +249,21 @@
} }
} }
// add rows to first tbody // add rows to first tbody
tc.$tbodies.eq(0).html( tds ); c.$tbodies.eq(0).html( tds );
} }
// only add new header text if the length matches // only add new header text if the length matches
if ( th && th.length === hl ) { if ( th && th.length === hl ) {
hsh = $t.hasClass('hasStickyHeaders'); hsh = $t.hasClass('hasStickyHeaders');
$sh = hsh ? tc.$sticky.children('thead:first').children().children() : ''; $sh = hsh ? c.$sticky.children('thead:first').children().children() : '';
$f = $t.find('tfoot tr:first').children(); $f = $t.find('tfoot tr:first').children();
$t.find('th.' + tc.cssHeader).each(function(j){ c.$headers.each(function(j){
var $t = $(this), icn; var $t = $(this), icn;
// add new test within the first span it finds, or just in the header // add new test within the first span it finds, or just in the header
if ( $t.find('.' + tc.cssIcon).length ) { if ( $t.find('.' + c.cssIcon).length ) {
icn = $t.find('.' + tc.cssIcon).clone(true); icn = $t.find('.' + c.cssIcon).clone(true);
$t.find('.tablesorter-header-inner').html( th[j] ).append(icn); $t.find('.tablesorter-header-inner').html( th[j] ).append(icn);
if ( hsh && $sh.length ) { if ( hsh && $sh.length ) {
icn = $sh.eq(j).find('.' + tc.cssIcon).clone(true); icn = $sh.eq(j).find('.' + c.cssIcon).clone(true);
$sh.eq(j).find('.tablesorter-header-inner').html( th[j] ).append(icn); $sh.eq(j).find('.tablesorter-header-inner').html( th[j] ).append(icn);
} }
} else { } else {
@ -273,55 +276,55 @@
}); });
} }
} }
if (tc.showProcessing) { if (c.showProcessing) {
$.tablesorter.isProcessing(table); // remove loading icon $.tablesorter.isProcessing(table); // remove loading icon
} }
$t.trigger('update'); $t.trigger('update');
c.totalPages = Math.ceil( c.totalRows / c.size ); p.totalPages = Math.ceil( p.totalRows / p.size );
updatePageDisplay(table, c); updatePageDisplay(table, p);
fixHeight(table, c); fixHeight(table, p);
if (c.initialized) { $t.trigger('pagerChange', c); } if (p.initialized) { $t.trigger('pagerChange', p); }
} }
if (!c.initialized) { if (!p.initialized) {
c.initialized = true; p.initialized = true;
$(table).trigger('pagerInitialized', c); c.$table.trigger('pagerInitialized', p);
} }
}, },
getAjax = function(table, c){ getAjax = function(table, p){
var url = getAjaxUrl(table, c), var url = getAjaxUrl(table, p),
$doc = $(document), $doc = $(document),
tc = table.config; c = table.config;
if ( url !== '' ) { if ( url !== '' ) {
if (tc.showProcessing) { if (c.showProcessing) {
$.tablesorter.isProcessing(table, true); // show loading icon $.tablesorter.isProcessing(table, true); // show loading icon
} }
$doc.bind('ajaxError.pager', function(e, xhr, settings, exception) { $doc.bind('ajaxError.pager', function(e, xhr, settings, exception) {
//show the error message on the table //show the error message on the table
if (url === settings.url) { if (url === settings.url) {
renderAjax(null, table, c, xhr, exception); renderAjax(null, table, p, xhr, exception);
$doc.unbind('ajaxError.pager'); $doc.unbind('ajaxError.pager');
} }
}); });
c.ajaxObject.url = url; // from the ajaxUrl option and modified by customAjaxUrl p.ajaxObject.url = url; // from the ajaxUrl option and modified by customAjaxUrl
c.ajaxObject.success = function(data) { p.ajaxObject.success = function(data) {
renderAjax(data, table, c); renderAjax(data, table, p);
$doc.unbind('ajaxError.pager'); $doc.unbind('ajaxError.pager');
if (typeof c.oldAjaxSuccess === 'function') { if (typeof p.oldAjaxSuccess === 'function') {
c.oldAjaxSuccess(data); p.oldAjaxSuccess(data);
} }
}; };
$.ajax(c.ajaxObject); $.ajax(p.ajaxObject);
} }
}, },
getAjaxUrl = function(table, c) { getAjaxUrl = function(table, p) {
var url = (c.ajaxUrl) ? c.ajaxUrl var url = (p.ajaxUrl) ? p.ajaxUrl
// allow using "{page+1}" in the url string to switch to a non-zero based index // 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(/\{page([\-+]\d+)?\}/, function(s,n){ return p.page + (n ? parseInt(n, 10) : 0); })
.replace(/\{size\}/g, c.size) : '', .replace(/\{size\}/g, p.size) : '',
sl = table.config.sortList, sl = table.config.sortList,
fl = c.currentFilters || [], fl = p.currentFilters || [],
sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/), sortCol = url.match(/\{\s*sort(?:List)?\s*:\s*(\w*)\s*\}/),
filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/), filterCol = url.match(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/),
arry = []; arry = [];
@ -344,22 +347,22 @@
// if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol" // if the arry is empty, just add the fcol parameter... "&{filterList:fcol}" becomes "&fcol"
url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol ); url = url.replace(/\{\s*filter(?:List)?\s*:\s*(\w*)\s*\}/g, arry.length ? arry.join('&') : filterCol );
} }
if ( typeof(c.customAjaxUrl) === "function" ) { if ( typeof(p.customAjaxUrl) === "function" ) {
url = c.customAjaxUrl(table, url); url = p.customAjaxUrl(table, url);
} }
return url; return url;
}, },
renderTable = function(table, rows, c) { renderTable = function(table, rows, p) {
c.isDisabled = false; // needed because sorting will change the page and re-enable the pager p.isDisabled = false; // needed because sorting will change the page and re-enable the pager
var i, j, o, $tb, var i, j, o, $tb,
l = rows.length, l = rows.length,
s = ( c.page * c.size ), s = ( p.page * p.size ),
e = ( s + c.size ); e = ( s + p.size );
if ( l < 1 ) { return; } // empty table, abort! if ( l < 1 ) { return; } // empty table, abort!
if (c.initialized) { $(table).trigger('pagerChange', c); } if (p.initialized) { $(table).trigger('pagerChange', p); }
if ( !c.removeRows ) { if ( !p.removeRows ) {
hideRows(table, c); hideRows(table, p);
} else { } else {
if ( e > rows.length ) { if ( e > rows.length ) {
e = rows.length; e = rows.length;
@ -375,116 +378,116 @@
} }
$.tablesorter.processTbody(table, $tb, false); $.tablesorter.processTbody(table, $tb, false);
} }
if ( c.page >= c.totalPages ) { if ( p.page >= p.totalPages ) {
moveToLastPage(table, c); moveToLastPage(table, p);
} }
updatePageDisplay(table, c); updatePageDisplay(table, p);
if ( !c.isDisabled ) { fixHeight(table, c); } if ( !p.isDisabled ) { fixHeight(table, p); }
$(table).trigger('applyWidgets'); $(table).trigger('applyWidgets');
}, },
showAllRows = function(table, c){ showAllRows = function(table, p){
if ( c.ajax ) { if ( p.ajax ) {
pagerArrows(c, true); pagerArrows(p, true);
} else { } else {
c.isDisabled = true; p.isDisabled = true;
$.data(table, 'pagerLastPage', c.page); $.data(table, 'pagerLastPage', p.page);
$.data(table, 'pagerLastSize', c.size); $.data(table, 'pagerLastSize', p.size);
c.page = 0; p.page = 0;
c.size = c.totalRows; p.size = p.totalRows;
c.totalPages = 1; p.totalPages = 1;
$(table).find('tr.pagerSavedHeightSpacer').remove(); $(table).find('tr.pagerSavedHeightSpacer').remove();
renderTable(table, table.config.rowsCopy, c); renderTable(table, table.config.rowsCopy, p);
} }
// disable size selector // disable size selector
c.$size.add(c.$goto).each(function(){ p.$size.add(p.$goto).each(function(){
$(this).addClass(c.cssDisabled)[0].disabled = true; $(this).addClass(p.cssDisabled)[0].disabled = true;
}); });
}, },
moveToPage = function(table, c, flag) { moveToPage = function(table, p, flag) {
if ( c.isDisabled ) { return; } if ( p.isDisabled ) { return; }
var p = Math.min( c.totalPages, c.filteredPages ); var pg = Math.min( p.totalPages, p.filteredPages );
if ( c.page < 0 ) { c.page = 0; } if ( p.page < 0 ) { p.page = 0; }
if ( c.page > ( p - 1 ) && p !== 0 ) { c.page = p - 1; } if ( p.page > ( pg - 1 ) && pg !== 0 ) { p.page = pg - 1; }
if (c.ajax) { if (p.ajax) {
getAjax(table, c); getAjax(table, p);
} else if (!c.ajax) { } else if (!p.ajax) {
renderTable(table, table.config.rowsCopy, c); renderTable(table, table.config.rowsCopy, p);
} }
$.data(table, 'pagerLastPage', c.page); $.data(table, 'pagerLastPage', p.page);
$.data(table, 'pagerUpdateTriggered', true); $.data(table, 'pagerUpdateTriggered', true);
if (c.initialized && flag !== false) { if (p.initialized && flag !== false) {
$(table).trigger('pageMoved', c); $(table).trigger('pageMoved', p);
} }
}, },
setPageSize = function(table, size, c) { setPageSize = function(table, size, p) {
c.size = size; p.size = size;
c.$size.val(size); p.$size.val(size);
$.data(table, 'pagerLastPage', c.page); $.data(table, 'pagerLastPage', p.page);
$.data(table, 'pagerLastSize', c.size); $.data(table, 'pagerLastSize', p.size);
c.totalPages = Math.ceil( c.totalRows / c.size ); p.totalPages = Math.ceil( p.totalRows / p.size );
moveToPage(table, c); moveToPage(table, p);
}, },
moveToFirstPage = function(table, c) { moveToFirstPage = function(table, p) {
c.page = 0; p.page = 0;
moveToPage(table, c); moveToPage(table, p);
}, },
moveToLastPage = function(table, c) { moveToLastPage = function(table, p) {
c.page = ( Math.min( c.totalPages, c.filteredPages ) - 1 ); p.page = ( Math.min( p.totalPages, p.filteredPages ) - 1 );
moveToPage(table, c); moveToPage(table, p);
}, },
moveToNextPage = function(table, c) { moveToNextPage = function(table, p) {
c.page++; p.page++;
if ( c.page >= ( Math.min( c.totalPages, c.filteredPages ) - 1 ) ) { if ( p.page >= ( Math.min( p.totalPages, p.filteredPages ) - 1 ) ) {
c.page = ( Math.min( c.totalPages, c.filteredPages ) - 1 ); p.page = ( Math.min( p.totalPages, p.filteredPages ) - 1 );
} }
moveToPage(table, c); moveToPage(table, p);
}, },
moveToPrevPage = function(table, c) { moveToPrevPage = function(table, p) {
c.page--; p.page--;
if ( c.page <= 0 ) { if ( p.page <= 0 ) {
c.page = 0; p.page = 0;
} }
moveToPage(table, c); moveToPage(table, p);
}, },
destroyPager = function(table, c){ destroyPager = function(table, p){
showAllRows(table, c); showAllRows(table, p);
c.$container.hide(); // hide pager p.$container.hide(); // hide pager
table.config.appender = null; // remove pager appender function table.config.appender = null; // remove pager appender function
$(table).unbind('destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager'); $(table).unbind('destroy.pager sortEnd.pager filterEnd.pager enable.pager disable.pager');
}, },
enablePager = function(table, c, triggered){ enablePager = function(table, p, triggered){
var p = c.$size.removeClass(c.cssDisabled).removeAttr('disabled'); var pg = p.$size.removeClass(p.cssDisabled).removeAttr('disabled');
c.$goto.removeClass(c.cssDisabled).removeAttr('disabled'); p.$goto.removeClass(p.cssDisabled).removeAttr('disabled');
c.isDisabled = false; p.isDisabled = false;
c.page = $.data(table, 'pagerLastPage') || c.page || 0; p.page = $.data(table, 'pagerLastPage') || p.page || 0;
c.size = $.data(table, 'pagerLastSize') || parseInt(p.find('option[selected]').val(), 10) || c.size; p.size = $.data(table, 'pagerLastSize') || parseInt(pg.find('option[selected]').val(), 10) || p.size;
p.val(c.size); // set page size pg.val(p.size); // set page size
c.totalPages = Math.ceil( Math.min( c.totalPages, c.filteredPages ) / c.size); p.totalPages = Math.ceil( Math.min( p.totalPages, p.filteredPages ) / p.size);
if ( triggered ) { if ( triggered ) {
$(table).trigger('update'); $(table).trigger('update');
setPageSize(table, c.size, c); setPageSize(table, p.size, p);
hideRowsSetup(table, c); hideRowsSetup(table, p);
fixHeight(table, c); fixHeight(table, p);
} }
}; };
$this.appender = function(table, rows) { $this.appender = function(table, rows) {
var c = table.config.pager; var p = table.config.pager;
if ( !c.ajax ) { if ( !p.ajax ) {
table.config.rowsCopy = rows; table.config.rowsCopy = rows;
c.totalRows = rows.length; p.totalRows = rows.length;
c.size = $.data(table, 'pagerLastSize') || c.size; p.size = $.data(table, 'pagerLastSize') || p.size;
c.totalPages = Math.ceil(c.totalRows / c.size); p.totalPages = Math.ceil(p.totalRows / p.size);
renderTable(table, rows, c); renderTable(table, rows, p);
} }
}; };
@ -493,21 +496,20 @@
// check if tablesorter has initialized // check if tablesorter has initialized
if (!(this.config && this.hasInitialized)) { return; } if (!(this.config && this.hasInitialized)) { return; }
var t, ctrls, fxn, var t, ctrls, fxn,
config = this.config,
c = config.pager = $.extend( {}, $.tablesorterPager.defaults, settings ),
table = this, table = this,
tc = table.config, c = table.config,
$t = $(table), p = c.pager = $.extend( {}, $.tablesorterPager.defaults, settings ),
$t = c.$table,
// added in case the pager is reinitialized after being destroyed. // added in case the pager is reinitialized after being destroyed.
pager = c.$container = $(c.container).addClass('tablesorter-pager').show(); pager = p.$container = $(p.container).addClass('tablesorter-pager').show();
c.oldAjaxSuccess = c.oldAjaxSuccess || c.ajaxObject.success; p.oldAjaxSuccess = p.oldAjaxSuccess || p.ajaxObject.success;
config.appender = $this.appender; c.appender = $this.appender;
$t $t
.unbind('filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager') .unbind('filterStart.pager filterEnd.pager sortEnd.pager disable.pager enable.pager destroy.pager update.pager pageSize.pager')
.bind('filterStart.pager', function(e, filters) { .bind('filterStart.pager', function(e, filters) {
$.data(table, 'pagerUpdateTriggered', false); $.data(table, 'pagerUpdateTriggered', false);
c.currentFilters = filters; p.currentFilters = filters;
}) })
// update pager after filter widget completes // update pager after filter widget completes
.bind('filterEnd.pager sortEnd.pager', function(e) { .bind('filterEnd.pager sortEnd.pager', function(e) {
@ -518,53 +520,53 @@
} }
//only run the server side sorting if it has been enabled //only run the server side sorting if it has been enabled
if (e.type === "filterEnd" || (e.type === "sortEnd" && tc.serverSideSorting)) { if (e.type === "filterEnd" || (e.type === "sortEnd" && tc.serverSideSorting)) {
moveToPage(table, c, false); moveToPage(table, p, false);
} }
updatePageDisplay(table, c, false); updatePageDisplay(table, p, false);
fixHeight(table, c); fixHeight(table, p);
}) })
.bind('disable.pager', function(e){ .bind('disable.pager', function(e){
e.stopPropagation(); e.stopPropagation();
showAllRows(table, c); showAllRows(table, p);
}) })
.bind('enable.pager', function(e){ .bind('enable.pager', function(e){
e.stopPropagation(); e.stopPropagation();
enablePager(table, c, true); enablePager(table, p, true);
}) })
.bind('destroy.pager', function(e){ .bind('destroy.pager', function(e){
e.stopPropagation(); e.stopPropagation();
destroyPager(table, c); destroyPager(table, p);
}) })
.bind('update.pager', function(e){ .bind('update.pager', function(e){
e.stopPropagation(); e.stopPropagation();
hideRows(table, c); hideRows(table, p);
}) })
.bind('pageSize.pager', function(e,v){ .bind('pageSize.pager', function(e,v){
e.stopPropagation(); e.stopPropagation();
setPageSize(table, parseInt(v, 10) || 10, c); setPageSize(table, parseInt(v, 10) || 10, p);
hideRows(table, c); hideRows(table, p);
updatePageDisplay(table, c, false); updatePageDisplay(table, p, false);
if (c.$size.length) { c.$size.val(c.size); } // twice? if (p.$size.length) { p.$size.val(p.size); } // twice?
}) })
.bind('pageSet.pager', function(e,v){ .bind('pageSet.pager', function(e,v){
e.stopPropagation(); e.stopPropagation();
c.page = (parseInt(v, 10) || 1) - 1; p.page = (parseInt(v, 10) || 1) - 1;
if (c.$goto.length) { c.$goto.val(c.size); } // twice? if (p.$goto.length) { p.$goto.val(p.size); } // twice?
moveToPage(table, c); moveToPage(table, p);
updatePageDisplay(table, c, false); updatePageDisplay(table, p, false);
}); });
// clicked controls // clicked controls
ctrls = [ c.cssFirst, c.cssPrev, c.cssNext, c.cssLast ]; ctrls = [ p.cssFirst, p.cssPrev, p.cssNext, p.cssLast ];
fxn = [ moveToFirstPage, moveToPrevPage, moveToNextPage, moveToLastPage ]; fxn = [ moveToFirstPage, moveToPrevPage, moveToNextPage, moveToLastPage ];
pager.find(ctrls.join(',')) pager.find(ctrls.join(','))
.unbind('click.pager') .unbind('click.pager')
.bind('click.pager', function(e){ .bind('click.pager', function(e){
var i, $t = $(this), l = ctrls.length; var i, $t = $(this), l = ctrls.length;
if ( !$t.hasClass(c.cssDisabled) ) { if ( !$t.hasClass(p.cssDisabled) ) {
for (i = 0; i < l; i++) { for (i = 0; i < l; i++) {
if ($t.is(ctrls[i])) { if ($t.is(ctrls[i])) {
fxn[i](table, c); fxn[i](table, p);
break; break;
} }
} }
@ -573,57 +575,57 @@
}); });
// goto selector // goto selector
c.$goto = pager.find(c.cssGoto); p.$goto = pager.find(p.cssGoto);
if ( c.$goto.length ) { if ( p.$goto.length ) {
c.$goto p.$goto
.unbind('change') .unbind('change')
.bind('change', function(){ .bind('change', function(){
c.page = $(this).val() - 1; p.page = $(this).val() - 1;
moveToPage(table, c); moveToPage(table, p);
}); });
updatePageDisplay(table, c, false); updatePageDisplay(table, p, false);
} }
// page size selector // page size selector
c.$size = pager.find(c.cssPageSize); p.$size = pager.find(p.cssPageSize);
if ( c.$size.length ) { if ( p.$size.length ) {
c.$size.unbind('change.pager').bind('change.pager', function() { p.$size.unbind('change.pager').bind('change.pager', function() {
c.$size.val( $(this).val() ); // in case there are more than one pagers p.$size.val( $(this).val() ); // in case there are more than one pagers
if ( !$(this).hasClass(c.cssDisabled) ) { if ( !$(this).hasClass(p.cssDisabled) ) {
setPageSize(table, parseInt( $(this).val(), 10 ), c); setPageSize(table, parseInt( $(this).val(), 10 ), p);
changeHeight(table, c); changeHeight(table, p);
} }
return false; return false;
}); });
} }
// clear initialized flag // clear initialized flag
c.initialized = false; p.initialized = false;
// before initialization event // before initialization event
$t.trigger('pagerBeforeInitialized', c); $t.trigger('pagerBeforeInitialized', p);
enablePager(table, c, false); enablePager(table, p, false);
if ( typeof(c.ajaxUrl) === 'string' ) { if ( typeof(p.ajaxUrl) === 'string' ) {
// ajax pager; interact with database // ajax pager; interact with database
c.ajax = true; p.ajax = true;
//When filtering with ajax, allow only custom filtering function, disable default filtering since it will be done server side. //When filtering with ajax, allow only custom filtering function, disable default filtering since it will be done server side.
tc.widgetOptions.filter_serversideFiltering = true; c.widgetOptions.filter_serversideFiltering = true;
tc.serverSideSorting = true; c.serverSideSorting = true;
moveToPage(table, c); moveToPage(table, p);
} else { } else {
c.ajax = false; p.ajax = false;
// Regular pager; all rows stored in memory // Regular pager; all rows stored in memory
$(this).trigger("appendCache", true); $(this).trigger("appendCache", true);
hideRowsSetup(table, c); hideRowsSetup(table, p);
} }
changeHeight(table, c); changeHeight(table, p);
// pager initialized // pager initialized
if (!c.ajax) { if (!p.ajax) {
c.initialized = true; p.initialized = true;
$(table).trigger('pagerInitialized', c); $(table).trigger('pagerInitialized', p);
} }
}); });
}; };