/*!
* tablesorter pager plugin
* updated 11/9/2013
*/
/*jshint browser:true, jquery:true, unused:false */
;(function($) {
"use strict";
/*jshint supernew:true */
var ts = $.tablesorter;
$.extend({ tablesorterPager: new function() {
this.defaults = {
// target the pager markup
container: null,
// 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.
// So a sortList = [[2,0],[3,0]] becomes "&col[2]=0&col[3]=0" in the url
// and a filterList = [[2,Blue],[3,13]] becomes "&fcol[2]=Blue&fcol[3]=13" in the url
ajaxUrl: null,
// modify the url after all processing has been applied
customAjaxUrl: function(table, url) { return url; },
// modify the $.ajax object to allow complete control over your ajax requests
ajaxObject: {
dataType: 'json'
},
// 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,
// Save pager page & size if the storage script is loaded (requires $.tablesorter.storage in jquery.tablesorter.widgets.js)
savePages: true,
// 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,
// count child rows towards the set page size? (set true if it is a visible table row within the pager)
// if true, child row(s) may not appear to be attached to its parent row, may be split across pages or
// may distort the table if rowspan or cellspans are included.
countChildRows: 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
cssErrorRow: 'tablesorter-errorRow', // error information row
// 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,
filteredPages: 0,
currentFilters: [],
startRow: 0,
endRow: 0,
$size: null,
last: {}
};
var $this = this,
// hide arrows at extremes
pagerArrows = function(p, disable) {
var a = 'addClass',
r = 'removeClass',
d = p.cssDisabled,
dis = !!disable,
tp = Math.min( p.totalPages, p.filteredPages );
if ( p.updateArrows ) {
p.$container.find(p.cssFirst + ',' + p.cssPrev)[ ( dis || p.page === 0 ) ? a : r ](d);
p.$container.find(p.cssNext + ',' + p.cssLast)[ ( dis || p.page === tp - 1 ) ? a : r ](d);
}
},
updatePageDisplay = function(table, p, flag) {
var i, pg, s, out,
c = table.config,
f = c.$table.hasClass('hasFilters') && !p.ajaxUrl,
t = (c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered') + ',' + c.selectorRemove,
sz = p.size || 10; // don't allow dividing by zero
p.totalPages = Math.ceil( p.totalRows / sz ); // needed for "pageSize" method
p.filteredRows = (f) ? c.$tbodies.eq(0).children('tr:not(.' + t + ')').length : p.totalRows;
p.filteredPages = (f) ? Math.ceil( p.filteredRows / sz ) || 1 : p.totalPages;
if ( Math.min( p.totalPages, p.filteredPages ) >= 0 ) {
t = (p.size * p.page > p.filteredRows);
p.startRow = (t) ? 1 : (p.filteredRows === 0 ? 0 : p.size * p.page + 1);
p.page = (t) ? 0 : p.page;
p.endRow = Math.min( p.filteredRows, p.totalRows, p.size * ( p.page + 1 ) );
out = p.$container.find(p.cssPageDisplay);
// form the output string (can now get a new output string from the server)
s = ( p.ajaxData && p.ajaxData.output ? p.ajaxData.output || p.output : p.output )
// {page} = one-based index; {page+#} = zero based index +/- value
.replace(/\{page([\-+]\d+)?\}/gi, function(m,n){
return p.page + (n ? parseInt(n, 10) : 1);
})
// {totalPages}, {extra}, {extra:0} (array) or {extra : key} (object)
.replace(/\{\w+(\s*:\s*\w+)?\}/gi, function(m){
var t = m.replace(/[{}\s]/g,''), a = t.split(':'), d = p.ajaxData;
return a.length > 1 && d && d[a[0]] ? d[a[0]][a[1]] : p[t] || (d ? d[t] : '') || '';
});
if (out.length) {
out[ (out[0].tagName === 'INPUT') ? 'val' : 'html' ](s);
if ( p.$goto.length ) {
t = '';
pg = Math.min( p.totalPages, p.filteredPages );
for ( i = 1; i <= pg; i++ ) {
t += '';
}
p.$goto.html(t).val( p.page + 1 );
}
}
}
pagerArrows(p);
if (p.initialized && flag !== false) {
c.$table.trigger('pagerComplete', p);
// save pager info to storage
if (p.savePages && ts.storage) {
ts.storage(table, 'tablesorter-pager', {
page : p.page,
size : p.size
});
}
}
},
fixHeight = function(table, p) {
var d, h,
c = table.config,
$b = c.$tbodies.eq(0);
if (p.fixedHeight) {
$b.find('tr.pagerSavedHeightSpacer').remove();
h = $.data(table, 'pagerSavedHeight');
if (h) {
d = h - $b.height();
if ( d > 5 && $.data(table, 'pagerLastSize') === p.size && $b.children('tr:visible').length < p.size ) {
$b.append('
');
}
}
}
},
changeHeight = function(table, p) {
var $b = table.config.$tbodies.eq(0);
$b.find('tr.pagerSavedHeightSpacer').remove();
$.data(table, 'pagerSavedHeight', $b.height());
fixHeight(table, p);
$.data(table, 'pagerLastSize', p.size);
},
hideRows = function(table, p){
if (!p.ajaxUrl) {
var i,
c = table.config,
rows = c.$tbodies.eq(0).children(),
l = rows.length,
s = ( p.page * p.size ),
e = s + p.size,
f = c.widgetOptions && c.widgetOptions.filter_filteredRow || 'filtered',
j = 0; // size counter
for ( i = 0; i < l; i++ ){
if ( !rows[i].className.match(f) ) {
rows[i].style.display = ( j >= s && j < e ) ? '' : 'none';
// don't count child rows
j += rows[i].className.match(c.cssChildRow + '|' + c.selectorRemove.slice(1)) && !p.countChildRows ? 0 : 1;
}
}
}
},
hideRowsSetup = function(table, p){
p.size = parseInt( p.$size.val(), 10 ) || p.size;
$.data(table, 'pagerLastSize', p.size);
pagerArrows(p);
if ( !p.removeRows ) {
hideRows(table, p);
$(table).bind('sortEnd.pager filterEnd.pager', function(){
hideRows(table, p);
});
}
},
renderAjax = function(data, table, p, xhr, exception){
// process data
if ( typeof(p.ajaxProcessing) === "function" ) {
// ajaxProcessing result: [ total, rows, headers ]
var i, j, hsh, $f, $sh, t, th, d, l, $err, rr_count,
c = table.config,
$t = c.$table,
tds = '',
result = p.ajaxProcessing(data, table) || [ 0, [] ],
hl = $t.find('thead th').length;
$t.find('thead tr.' + p.cssErrorRow).remove(); // Clean up any previous error.
if ( exception ) {
if (c.debug) {
ts.log('Ajax Error', xhr, exception);
}
$err = $('