Core & pager: replace remaining jQuery each function. Fixes #827

This commit is contained in:
Mottie 2015-05-17 11:10:49 -05:00
parent ed321c20bd
commit 8d2abbba32
3 changed files with 132 additions and 97 deletions

View File

@ -377,12 +377,12 @@
// process data // process data
if ( typeof(p.ajaxProcessing) === "function" ) { if ( typeof(p.ajaxProcessing) === "function" ) {
// ajaxProcessing result: [ total, rows, headers ] // ajaxProcessing result: [ total, rows, headers ]
var i, j, hsh, $f, $sh, t, th, d, l, rr_count, var i, j, t, hsh, $f, $sh, $headers, $h, icon, th, d, l, rr_count, len,
c = table.config, c = table.config,
$t = c.$table, $table = c.$table,
tds = '', tds = '',
result = p.ajaxProcessing(data, table, xhr) || [ 0, [] ], result = p.ajaxProcessing(data, table, xhr) || [ 0, [] ],
hl = $t.find('thead th').length; hl = $table.find('thead th').length;
// Clean up any previous error. // Clean up any previous error.
ts.showError(table); ts.showError(table);
@ -445,28 +445,30 @@
p.processAjaxOnInit = true; p.processAjaxOnInit = true;
// 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 = $table.hasClass('hasStickyHeaders');
$sh = hsh ? c.widgetOptions.$sticky.children('thead:first').children('tr').children() : ''; $sh = hsh ? c.widgetOptions.$sticky.children('thead:first').children('tr').children() : '';
$f = $t.find('tfoot tr:first').children(); $f = $table.find('tfoot tr:first').children();
// don't change td headers (may contain pager) // don't change td headers (may contain pager)
c.$headers.filter('th').each(function(j){ $headers = c.$headers.filter( 'th ' );
var $t = $(this), icn; len = $headers.length;
for ( j = 0; j < len; j++ ) {
$h = $headers.eq( j );
// 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('.' + ts.css.icon).length ) { if ( $h.find('.' + ts.css.icon).length ) {
icn = $t.find('.' + ts.css.icon).clone(true); icon = $h.find('.' + ts.css.icon).clone(true);
$t.find('.tablesorter-header-inner').html( th[j] ).append(icn); $h.find('.tablesorter-header-inner').html( th[j] ).append(icon);
if ( hsh && $sh.length ) { if ( hsh && $sh.length ) {
icn = $sh.eq(j).find('.' + ts.css.icon).clone(true); icon = $sh.eq(j).find('.' + ts.css.icon).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(icon);
} }
} else { } else {
$t.find('.tablesorter-header-inner').html( th[j] ); $h.find('.tablesorter-header-inner').html( th[j] );
if (hsh && $sh.length) { if (hsh && $sh.length) {
$sh.eq(j).find('.tablesorter-header-inner').html( th[j] ); $sh.eq(j).find('.tablesorter-header-inner').html( th[j] );
} }
} }
$f.eq(j).html( th[j] ); $f.eq(j).html( th[j] );
}); }
} }
} }
if (c.showProcessing) { if (c.showProcessing) {
@ -479,7 +481,7 @@
p.last.currentFilters = p.currentFilters; p.last.currentFilters = p.currentFilters;
p.last.sortList = (c.sortList || []).join(','); p.last.sortList = (c.sortList || []).join(',');
updatePageDisplay(table, p, false); updatePageDisplay(table, p, false);
$t.trigger('updateCache', [function(){ $table.trigger('updateCache', [function(){
if (p.initialized) { if (p.initialized) {
// apply widgets after table has rendered & after a delay to prevent // apply widgets after table has rendered & after a delay to prevent
// multiple applyWidget blocking code from blocking this trigger // multiple applyWidget blocking code from blocking this trigger
@ -487,7 +489,7 @@
if (c.debug) { if (c.debug) {
ts.log('Pager: Triggering pagerChange'); ts.log('Pager: Triggering pagerChange');
} }
$t $table
.trigger('applyWidgets') .trigger('applyWidgets')
.trigger('pagerChange', p); .trigger('pagerChange', p);
updatePageDisplay(table, p, true); updatePageDisplay(table, p, true);
@ -649,6 +651,7 @@
}, },
showAllRows = function(table, p){ showAllRows = function(table, p){
var index, $controls, len;
if ( p.ajax ) { if ( p.ajax ) {
pagerArrows(p, true); pagerArrows(p, true);
} else { } else {
@ -669,9 +672,15 @@
} }
} }
// disable size selector // disable size selector
p.$size.add(p.$goto).add(p.$container.find('.ts-startRow, .ts-page')).each(function(){ $controls = p.$size
$(this).attr('aria-disabled', 'true').addClass(p.cssDisabled)[0].disabled = true; .add( p.$goto )
}); .add( p.$container.find( '.ts-startRow, .ts-page' ) );
len = $controls.length;
for ( index = 0; index < len; index++ ) {
$controls.eq( index )
.attr( 'aria-disabled', 'true' )
.addClass( p.cssDisabled )[0].disabled = true;
}
}, },
// updateCache if delayInit: true // updateCache if delayInit: true
@ -1043,13 +1052,15 @@
}() }); }() });
// see #486 // see #486
ts.showError = function(table, message){ ts.showError = function(table, message) {
$(table).each(function(){ var index, $row, c, errorRow,
var $row, $table = $( table ),
c = this.config, len = $table.length;
for ( index = 0; index < len; index++ ) {
c = $table[ index ].config;
if ( c ) {
errorRow = c.pager && c.pager.cssErrorRow || c.widgetOptions.pager_css && c.widgetOptions.pager_css.errorRow || 'tablesorter-errorRow'; errorRow = c.pager && c.pager.cssErrorRow || c.widgetOptions.pager_css && c.widgetOptions.pager_css.errorRow || 'tablesorter-errorRow';
if (c) { if ( typeof message === 'undefined' ) {
if (typeof message === 'undefined') {
c.$table.find('thead').find(c.selectorRemove).remove(); c.$table.find('thead').find(c.selectorRemove).remove();
} else { } else {
$row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') ) $row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') )
@ -1065,7 +1076,7 @@
}); });
} }
} }
}); }
}; };
// extend plugin scope // extend plugin scope

View File

@ -554,14 +554,15 @@
} }
function updateHeader(table) { function updateHeader(table) {
var s, $th, col, var index, s, $th, col,
c = table.config; c = table.config,
c.$headers.each(function(index, th){ len = c.$headers.length;
$th = $(th); for ( index = 0; index < len; index++ ) {
$th = c.$headers.eq( index );
col = ts.getColumnData( table, c.headers, index, true ); col = ts.getColumnData( table, c.headers, index, true );
// add 'sorter-false' class if 'parser-false' is set // add 'sorter-false' class if 'parser-false' is set
s = ts.getData( th, col, 'sorter' ) === 'false' || ts.getData( th, col, 'parser' ) === 'false'; s = ts.getData( $th, col, 'sorter' ) === 'false' || ts.getData( $th, col, 'parser' ) === 'false';
th.sortDisabled = s; $th[0].sortDisabled = s;
$th[ s ? 'addClass' : 'removeClass' ]('sorter-false').attr('aria-disabled', '' + s); $th[ s ? 'addClass' : 'removeClass' ]('sorter-false').attr('aria-disabled', '' + s);
// aria-controls - requires table ID // aria-controls - requires table ID
if (table.id) { if (table.id) {
@ -571,11 +572,11 @@
$th.attr('aria-controls', table.id); $th.attr('aria-controls', table.id);
} }
} }
}); }
} }
function setHeadersCss(table) { function setHeadersCss(table) {
var f, i, j, var f, h, i, j, $headers, $h, nextSort, txt,
c = table.config, c = table.config,
list = c.sortList, list = c.sortList,
len = list.length, len = list.length,
@ -619,14 +620,19 @@
} }
} }
// add verbose aria labels // add verbose aria labels
c.$headers.not('.sorter-false').each(function(){ len = c.$headers.length;
var $this = $(this), $headers = c.$headers.not('.sorter-false');
nextSort = this.order[(this.count + 1) % (c.sortReset ? 3 : 2)], for ( i = 0; i < len; i++ ) {
txt = $.trim( $this.text() ) + ': ' + $h = $headers.eq( i );
ts.language[ $this.hasClass(ts.css.sortAsc) ? 'sortAsc' : $this.hasClass(ts.css.sortDesc) ? 'sortDesc' : 'sortNone' ] + if ( $h.length ) {
h = $headers[ i ];
nextSort = h.order[ ( h.count + 1 ) % ( c.sortReset ? 3 : 2 ) ],
txt = $.trim( $h.text() ) + ': ' +
ts.language[ $h.hasClass( ts.css.sortAsc ) ? 'sortAsc' : $h.hasClass( ts.css.sortDesc ) ? 'sortDesc' : 'sortNone' ] +
ts.language[ nextSort === 0 ? 'nextAsc' : nextSort === 1 ? 'nextDesc' : 'nextNone' ]; ts.language[ nextSort === 0 ? 'nextAsc' : nextSort === 1 ? 'nextDesc' : 'nextNone' ];
$this.attr('aria-label', txt ); $h.attr( 'aria-label', txt );
}); }
}
} }
function updateHeaderSortCount( table, list ) { function updateHeaderSortCount( table, list ) {
@ -686,10 +692,11 @@
// let any updates complete before initializing a sort // let any updates complete before initializing a sort
return setTimeout(function(){ initSort(table, cell, event); }, 50); return setTimeout(function(){ initSort(table, cell, event); }, 50);
} }
var arry, indx, col, order, s, var arry, indx, i, col, order, s, $header,
c = table.config, c = table.config,
key = !event[c.sortMultiSortKey], key = !event[c.sortMultiSortKey],
$table = c.$table; $table = c.$table,
len = c.$headers.length;
// Only call sortStart if sorting is enabled // Only call sortStart if sorting is enabled
$table.trigger('sortStart', table); $table.trigger('sortStart', table);
// get current column sort order // get current column sort order
@ -697,12 +704,13 @@
// reset all sorts on non-current column - issue #30 // reset all sorts on non-current column - issue #30
if (c.sortRestart) { if (c.sortRestart) {
indx = cell; indx = cell;
c.$headers.each(function() { for ( i = 0; i < len; i++ ) {
$header = c.$headers.eq( i );
// only reset counts on columns that weren't just clicked on and if not included in a multisort // only reset counts on columns that weren't just clicked on and if not included in a multisort
if (this !== indx && (key || !$(this).is('.' + ts.css.sortDesc + ',.' + ts.css.sortAsc))) { if ( $header[0] !== indx && ( key || !$header.is('.' + ts.css.sortDesc + ',.' + ts.css.sortAsc) ) ) {
this.count = -1; $header[0].count = -1;
} }
}); }
} }
// get current column index // get current column index
indx = parseInt( $(cell).attr('data-column'), 10 ); indx = parseInt( $(cell).attr('data-column'), 10 );
@ -1218,7 +1226,7 @@
// automatically add a colgroup with col elements set to a percentage width // automatically add a colgroup with col elements set to a percentage width
ts.fixColumnWidth = function(table) { ts.fixColumnWidth = function(table) {
table = $(table)[0]; table = $(table)[0];
var overallWidth, percent, var overallWidth, percent, $tbodies, len, index,
c = table.config, c = table.config,
colgroup = c.$table.children('colgroup'); colgroup = c.$table.children('colgroup');
// remove plugin-added colgroup, in case we need to refresh the widths // remove plugin-added colgroup, in case we need to refresh the widths
@ -1229,10 +1237,12 @@
colgroup = $('<colgroup class="' + ts.css.colgroup + '">'); colgroup = $('<colgroup class="' + ts.css.colgroup + '">');
overallWidth = c.$table.width(); overallWidth = c.$table.width();
// only add col for visible columns - fixes #371 // only add col for visible columns - fixes #371
c.$tbodies.find('tr:first').children(':visible').each(function() { $tbodies = c.$tbodies.find('tr:first').children(':visible'); //.each(function()
percent = parseInt( ( $(this).width() / overallWidth ) * 1000, 10 ) / 10 + '%'; len = $tbodies.length;
for ( index = 0; index < len; index++ ) {
percent = parseInt( ( $tbodies.eq( index ).width() / overallWidth ) * 1000, 10 ) / 10 + '%';
colgroup.append( $('<col>').css('width', percent) ); colgroup.append( $('<col>').css('width', percent) );
}); }
c.$table.prepend(colgroup); c.$table.prepend(colgroup);
} }
}; };
@ -1424,17 +1434,20 @@
// restore headers // restore headers
ts.restoreHeaders = function(table){ ts.restoreHeaders = function(table){
var $cell, var index, $cell,
c = $(table)[0].config; c = $(table)[0].config,
$headers = c.$table.find( c.selectorHeaders ),
len = $headers.length;
// don't use c.$headers here in case header cells were swapped // don't use c.$headers here in case header cells were swapped
c.$table.find(c.selectorHeaders).each(function(i){ for ( index = 0; index < len; index++ ) {
$cell = $(this); // c.$table.find(c.selectorHeaders).each(function(i){
$cell = $headers.eq( index );
// only restore header cells if it is wrapped // only restore header cells if it is wrapped
// because this is also used by the updateAll method // because this is also used by the updateAll method
if ($cell.find('.' + ts.css.headerIn).length){ if ( $cell.find( '.' + ts.css.headerIn ).length ) {
$cell.html( c.headerContent[i] ); $cell.html( c.headerContent[ index ] );
} }
}); }
}; };
ts.destroy = function(table, removeClasses, callback){ ts.destroy = function(table, removeClasses, callback){
@ -2121,7 +2134,7 @@
id: 'zebra', id: 'zebra',
priority: 90, priority: 90,
format: function(table, c, wo) { format: function(table, c, wo) {
var $tb, $tv, $tr, row, even, time, k, var $tv, $tr, row, even, time, k, i, len,
child = new RegExp(c.cssChildRow, 'i'), child = new RegExp(c.cssChildRow, 'i'),
b = c.$tbodies.add( $( c.namespace + '_extra_table' ).children( 'tbody' ) ); b = c.$tbodies.add( $( c.namespace + '_extra_table' ).children( 'tbody' ) );
if (c.debug) { if (c.debug) {
@ -2130,17 +2143,17 @@
for (k = 0; k < b.length; k++ ) { for (k = 0; k < b.length; k++ ) {
// loop through the visible rows // loop through the visible rows
row = 0; row = 0;
$tb = b.eq(k); $tv = b.eq( k ).children( 'tr:visible' ).not( c.selectorRemove );
$tv = $tb.children('tr:visible').not(c.selectorRemove); len = $tv.length;
// revered back to using jQuery each - strangely it's the fastest method for ( i = 0; i < len; i++ ) {
/*jshint loopfunc:true */ $tr = $tv.eq( i );
$tv.each(function(){
$tr = $(this);
// style child rows the same way the parent row was styled // style child rows the same way the parent row was styled
if (!child.test(this.className)) { row++; } if ( !child.test( $tr[0].className ) ) { row++; }
even = (row % 2 === 0); even = ( row % 2 === 0 );
$tr.removeClass(wo.zebra[even ? 1 : 0]).addClass(wo.zebra[even ? 0 : 1]); $tr
}); .removeClass( wo.zebra[ even ? 1 : 0 ] )
.addClass( wo.zebra[ even ? 0 : 1 ] );
}
} }
}, },
remove: function(table, c, wo, refreshing){ remove: function(table, c, wo, refreshing){

View File

@ -638,11 +638,11 @@ tsp = ts.pager = {
// process data // process data
if ( $.isFunction(wo.pager_ajaxProcessing) ) { if ( $.isFunction(wo.pager_ajaxProcessing) ) {
// ajaxProcessing result: [ total, rows, headers ] // ajaxProcessing result: [ total, rows, headers ]
var i, j, t, hsh, $f, $sh, th, d, l, rr_count, var i, j, t, hsh, $f, $sh, $headers, $h, icon, th, d, l, rr_count, len,
$t = c.$table, $table = c.$table,
tds = '', tds = '',
result = wo.pager_ajaxProcessing(data, table, xhr) || [ 0, [] ], result = wo.pager_ajaxProcessing(data, table, xhr) || [ 0, [] ],
hl = $t.find('thead th').length; hl = $table.find('thead th').length;
// Clean up any previous error. // Clean up any previous error.
ts.showError(table); ts.showError(table);
@ -698,28 +698,30 @@ tsp = ts.pager = {
wo.pager_processAjaxOnInit = true; wo.pager_processAjaxOnInit = true;
// 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 = $table.hasClass('hasStickyHeaders');
$sh = hsh ? wo.$sticky.children('thead:first').children('tr').children() : ''; $sh = hsh ? wo.$sticky.children('thead:first').children('tr').children() : '';
$f = $t.find('tfoot tr:first').children(); $f = $table.find('tfoot tr:first').children();
// don't change td headers (may contain pager) // don't change td headers (may contain pager)
c.$headers.filter('th').each(function(j){ $headers = c.$headers.filter( 'th ');
var $t = $(this), icn; len = $headers.length;
for ( j = 0; j < len; j++ ) {
$h = $headers.eq( j );
// 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('.' + ts.css.icon).length ) { if ( $h.find('.' + ts.css.icon).length ) {
icn = $t.find('.' + ts.css.icon).clone(true); icon = $h.find('.' + ts.css.icon).clone(true);
$t.find('.tablesorter-header-inner').html( th[j] ).append(icn); $h.find('.tablesorter-header-inner').html( th[j] ).append(icon);
if ( hsh && $sh.length ) { if ( hsh && $sh.length ) {
icn = $sh.eq(j).find('.' + ts.css.icon).clone(true); icon = $sh.eq(j).find('.' + ts.css.icon).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(icon);
} }
} else { } else {
$t.find('.tablesorter-header-inner').html( th[j] ); $h.find('.tablesorter-header-inner').html( th[j] );
if (hsh && $sh.length) { if (hsh && $sh.length) {
$sh.eq(j).find('.tablesorter-header-inner').html( th[j] ); $sh.eq(j).find('.tablesorter-header-inner').html( th[j] );
} }
} }
$f.eq(j).html( th[j] ); $f.eq(j).html( th[j] );
}); }
} }
} }
if (c.showProcessing) { if (c.showProcessing) {
@ -734,7 +736,7 @@ tsp = ts.pager = {
p.initializing = false; p.initializing = false;
// update display without triggering pager complete... before updating cache // update display without triggering pager complete... before updating cache
tsp.updatePageDisplay(table, c, false); tsp.updatePageDisplay(table, c, false);
$t.trigger('updateCache', [function(){ $table.trigger('updateCache', [function(){
if (p.initialized) { if (p.initialized) {
// apply widgets after table has rendered & after a delay to prevent // apply widgets after table has rendered & after a delay to prevent
// multiple applyWidget blocking code from blocking this trigger // multiple applyWidget blocking code from blocking this trigger
@ -742,7 +744,7 @@ tsp = ts.pager = {
if (c.debug) { if (c.debug) {
ts.log('Pager: Triggering pagerChange'); ts.log('Pager: Triggering pagerChange');
} }
$t $table
.trigger('applyWidgets') .trigger('applyWidgets')
.trigger('pagerChange', p); .trigger('pagerChange', p);
tsp.updatePageDisplay(table, c); tsp.updatePageDisplay(table, c);
@ -898,7 +900,8 @@ tsp = ts.pager = {
}, },
showAllRows: function(table, c){ showAllRows: function(table, c){
var p = c.pager, var index, $controls, len,
p = c.pager,
wo = c.widgetOptions; wo = c.widgetOptions;
if ( p.ajax ) { if ( p.ajax ) {
tsp.pagerArrows(c, true); tsp.pagerArrows(c, true);
@ -920,9 +923,15 @@ tsp = ts.pager = {
} }
} }
// disable size selector // disable size selector
p.$size.add(p.$goto).each(function(){ $controls = p.$size
$(this).attr('aria-disabled', 'true').addClass(wo.pager_css.disabled)[0].disabled = true; .add( p.$goto )
}); .add( p.$container.find( '.ts-startRow, .ts-page ' ) );
len = $controls.length;
for ( index = 0; index < len; index++ ) {
$controls.eq( index )
.attr( 'aria-disabled', 'true' )
.addClass( wo.pager_css.disabled )[0].disabled = true;
}
}, },
// updateCache if delayInit: true // updateCache if delayInit: true
@ -1109,14 +1118,16 @@ tsp = ts.pager = {
}; };
// see #486 // see #486
ts.showError = function(table, message){ ts.showError = function( table, message ) {
$(table).each(function(){ var index, $row, c, wo, errorRow,
var $row, $table = $( table ),
c = this.config, len = $table.length;
wo = c.widgetOptions, for ( index = 0; index < len; index++ ) {
c = $table[ index ].config;
if ( c ) {
wo = c.widgetOptions;
errorRow = c.pager && c.pager.cssErrorRow || wo.pager_css && wo.pager_css.errorRow || 'tablesorter-errorRow'; errorRow = c.pager && c.pager.cssErrorRow || wo.pager_css && wo.pager_css.errorRow || 'tablesorter-errorRow';
if (c) { if ( typeof message === 'undefined' ) {
if (typeof message === 'undefined') {
c.$table.find('thead').find(c.selectorRemove).remove(); c.$table.find('thead').find(c.selectorRemove).remove();
} else { } else {
$row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') ) $row = ( /tr\>/.test(message) ? $(message) : $('<tr><td colspan="' + c.columns + '">' + message + '</td></tr>') )
@ -1132,7 +1143,7 @@ ts.showError = function(table, message){
}); });
} }
} }
}); }
}; };
})(jQuery); })(jQuery);