Math: lots of tweaks. See #1083

- Initial calculation performed once - changed binding to either "filterEnd"
  or "pagerComplete", not both.
- Added change flag so a cache update is only performed when cell content
  inside of a sortable tbody was modified.
- Fix data-math-filter on "all" cell calculation with fixes to getRow &
  getColumn functions so that filters get priority over the "filtered" row
  check.
This commit is contained in:
Rob Garrison 2015-12-12 20:41:27 -06:00
parent 3ada0de10a
commit 4e1c96d437
2 changed files with 150 additions and 80 deletions

View File

@ -18,7 +18,7 @@
<!-- Tablesorter: required --> <!-- Tablesorter: required -->
<link rel="stylesheet" href="../css/theme.blue.css"> <link rel="stylesheet" href="../css/theme.blue.css">
<script src="../js/jquery.tablesorter.js"></script> <script src="../js/jquery.tablesorter.js"></script>
<script src="../js/jquery.tablesorter.widgets.js"></script> <script src="../js/widgets/widget-filter.js"></script>
<script src="../js/widgets/widget-math.js"></script> <script src="../js/widgets/widget-math.js"></script>
@ -182,7 +182,15 @@
<li>In <span class="version">v2.24.7</span> <li>In <span class="version">v2.24.7</span>
<ul> <ul>
<li><code>math_rowFilter</code> can now be overridden by the row's <code>data-math-filter</code> attribute.</li> <li><code>math_rowFilter</code> can now be overridden by the row's <code>data-math-filter</code> attribute.</li>
<li>On initialization, only tbody cells with a <code>rowspan</code> or <code>colspan</code> are now processed and only get a "data-column" set if the internal <code>cellIndex</code> doesn't match the calculated cell index. This should improve performance and reduce lag after initialization and updating the table (see <a href="https://github.com/Mottie/tablesorter/issues/1048">issue #1048</a>.</li> <li>
Lots of optimizations!
<ul>
<li>On initialization, only tbody cells with a <code>rowspan</code> or <code>colspan</code> are now processed and only get a "data-column" set if the internal <code>cellIndex</code> doesn't match the calculated cell index.</li>
<li>Initial calculation is no longer performed three times in a row</li>
<li>The cache is now only updated if cell content inside of a sortable tbody was modified.</li>
<li>These changes should improve performance and reduce lag after initialization and updating the table (see <a href="https://github.com/Mottie/tablesorter/issues/1048">issue #1048</a>.</li>
</ul>
</li>
</ul> </ul>
</li> </li>
<li>In <span class="version">v2.24.6</span>, added <code>math_rowFilter</code> option.</li> <li>In <span class="version">v2.24.6</span>, added <code>math_rowFilter</code> option.</li>
@ -192,25 +200,36 @@
<li>Added <code>math_none</code> option which allows customizing the text added to a cell when there are no matching math elements (.</li> <li>Added <code>math_none</code> option which allows customizing the text added to a cell when there are no matching math elements (.</li>
</ul> </ul>
</li> </li>
<li>In <span class="version updated">v2.22.0</span>, </ul>
<div class="accordion start-closed">
<h3 id="old-notes"><a href="#">Older Notes</a></h3>
<div>
<ul> <ul>
<li>Fixed an issue with a sum column encountering a cell without a defined "data-math" attribute returning an empty string instead of zero. See <a href="https://github.com/Mottie/tablesorter/issues/873">issue #873</a>.</li> <li>In <span class="version updated">v2.22.0</span>,
<li>Fixed a javascript error occurring on empty tables & now all updates will reapply column indexing to tbody cells - I know this isn't ideal as it would be slow on larger tables.</li> <ul>
<li>Fixed an issue with a sum column encountering a cell without a defined "data-math" attribute returning an empty string instead of zero. See <a href="https://github.com/Mottie/tablesorter/issues/873">issue #873</a>.</li>
<li>Fixed a javascript error occurring on empty tables & now all updates will reapply column indexing to tbody cells - I know this isn't ideal as it would be slow on larger tables.</li>
</ul>
</li>
<li>In <span class="version">v2.19.1</span>, added <code>math_event</code> option &amp; fixed an issue with event unbinding in jQuery version &lt; 1.9.</li>
<li>In <span class="version">v2.17.1</span>,
<ul>
<li>Values added to the data-attribute set by the <a href="../#textattribute"><code>textAttribute</code> option</a> will now be used in the calculation instead of the actual cell content.</li>
<li>The Grand Total cells now shows a higher precision value to emphasize this point.</li>
</ul>
</li>
<li>In <span class="version">v2.16.4</span>, added:
<ul>
<li>Two new options: <code>math_prefix</code> and <code>math_suffix</code>, which will be added before or after the prefix or suffix, respectively.</li>
<li>Added "Mask Examples" section with examples, and how to use the <code>$.tablesorter.formatMask</code> function.</li>
</ul>
</li>
</ul> </ul>
</li> </div>
<li>In <span class="version">v2.19.1</span>, added <code>math_event</code> option &amp; fixed an issue with event unbinding in jQuery version &lt; 1.9.</li> </div>
<li>In <span class="version">v2.17.1</span>,
<ul> <ul>
<li>Values added to the data-attribute set by the <a href="../#textattribute"><code>textAttribute</code> option</a> will now be used in the calculation instead of the actual cell content.</li>
<li>The Grand Total cells now shows a higher precision value to emphasize this point.</li>
</ul>
</li>
<li>In <span class="version">v2.16.4</span>, added:
<ul>
<li>Two new options: <code>math_prefix</code> and <code>math_suffix</code>, which will be added before or after the prefix or suffix, respectively.</li>
<li>Added "Mask Examples" section with examples, and how to use the <code>$.tablesorter.formatMask</code> function.</li>
</ul><br>
</li>
<li>This widget will <strong>only work</strong> in tablesorter version 2.16+ and jQuery version 1.7+.</li> <li>This widget will <strong>only work</strong> in tablesorter version 2.16+ and jQuery version 1.7+.</li>
<li>It adds basic math capabilities. A full list of default formulas is listed in the "Attribute Settings" section.</li> <li>It adds basic math capabilities. A full list of default formulas is listed in the "Attribute Settings" section.</li>
<li>Add your own custom formulas which manipulating an array of values gathered from the table by row, column or a column block (above).</li> <li>Add your own custom formulas which manipulating an array of values gathered from the table by row, column or a column block (above).</li>
@ -218,7 +237,7 @@
<li>The widget will update the calculations based on filtered rows, and will update if any data within the table changes (using update events).</li> <li>The widget will update the calculations based on filtered rows, and will update if any data within the table changes (using update events).</li>
<li>This widget is not optimized for very large tables, for two reasons: <li>This widget is not optimized for very large tables, for two reasons:
<ul> <ul>
<li>On initialization, it cycles through every table row, calculates the column index<del>, and adds a <code>data-column</code> attribute</del> (Fixed in <span class="version updated">v2.24.7</span>).</li> <li>On initialization, it cycles through every table row, calculates the column index <del>, and adds a <code>data-column</code> attribute</del> (Fixed in <span class="version updated">v2.24.7</span>).</li>
<li>It uses the update method whenever it recalculates values to make the results sortable. This occurs when any of the update methods are used and after the table is filtered.</li> <li>It uses the update method whenever it recalculates values to make the results sortable. This occurs when any of the update methods are used and after the table is filtered.</li>
</ul> </ul>
</li> </li>
@ -442,6 +461,7 @@ math_suffix : '{content}<span class="red">!</span>'
math_rowFilter : ':visible:not(.filtered)'</pre> math_rowFilter : ':visible:not(.filtered)'</pre>
In <span class="version">v2.24.7</span>, this setting my be overridden in specific rows by adding a <code>data-math-filter</code> attribute (The "math" portion is set by the <code>math_data</code> option) In <span class="version">v2.24.7</span>, this setting my be overridden in specific rows by adding a <code>data-math-filter</code> attribute (The "math" portion is set by the <code>math_data</code> option)
<pre class="prettyprint lang-html">&lt;tr data-math-filter=":visible"&gt;...&lt;/tr&gt;</pre> <pre class="prettyprint lang-html">&lt;tr data-math-filter=":visible"&gt;...&lt;/tr&gt;</pre>
<span class="label label-info">Note</span> The <code>data-math-filter</code> attribute is ignored if set to an empty string; instead use <code>data-math-filter="*"</code> to target all rows as done in the first example on this page.
</div> </div>
</td> </td>
</tr> </tr>
@ -580,9 +600,17 @@ BAD =&gt; No minus (-) here! $#,###.00 or [-] here either &lt;= BAD</textarea>
<th data-math="col-sum">col-sum</th> <th data-math="col-sum">col-sum</th>
<th data-math="col-sum">col-sum</th> <th data-math="col-sum">col-sum</th>
</tr> </tr>
<tr>
<th colspan="5">Visible Total</th>
<th data-math="all-sum"></th>
</tr>
<tr> <tr>
<th colspan="5">Grand Total</th> <th colspan="5">Grand Total</th>
<th data-math="all-sum" data-math-mask="##0.0000">all-sum</th> <!--
The "data-math-filter" attribute can not be set to an empty string AND override the widgetOptions.math_rowFilter,
so set to to an asterisk ("*") to target all rows.
-->
<th data-math="all-sum" data-math-filter="*" data-math-mask="##0.0000"></th>
</tr> </tr>
</tfoot> </tfoot>

View File

@ -25,8 +25,8 @@
return c && c.widgetOptions.math_none || 'none'; // text for cell return c && c.widgetOptions.math_none || 'none'; // text for cell
}, },
events : ( 'tablesorter-initialized update updateAll updateRows addRows updateCell ' + events : ( 'tablesorter-initialized update updateAll updateRows addRows updateCell filterReset ' )
'filterReset filterEnd ' ).split(' ').join('.tsmath '), .split(' ').join('.tsmath '),
processText : function( c, $cell ) { processText : function( c, $cell ) {
var txt = ts.getElementText( c, $cell, math.getCellIndex( $cell ) ); var txt = ts.getElementText( c, $cell, math.getCellIndex( $cell ) );
@ -36,17 +36,16 @@
}, },
// get all of the row numerical values in an arry // get all of the row numerical values in an arry
getRow : function( c, $el ) { getRow : function( c, $el, hasFilter ) {
var $cells, var $cells,
wo = c.widgetOptions, wo = c.widgetOptions,
arry = [], arry = [],
$row = $el.closest( 'tr' ), $row = $el.closest( 'tr' ),
isFiltered = $row.hasClass( wo.filter_filteredRow || 'filtered' ), isFiltered = $row.hasClass( wo.filter_filteredRow || 'filtered' );
hasFilter = $row.attr( wo.math_dataAttrib + '-filter' ) || wo.math_rowFilter;
if ( hasFilter ) { if ( hasFilter ) {
$row = $row.filter( hasFilter ); $row = $row.filter( hasFilter );
} }
if ( !isFiltered || hasFilter ) { if ( hasFilter || !isFiltered ) {
$cells = $row.children().not( '[' + wo.math_dataAttrib + '=ignore]' ); $cells = $row.children().not( '[' + wo.math_dataAttrib + '=ignore]' );
if ( wo.math_ignore.length ) { if ( wo.math_ignore.length ) {
$cells = $cells.filter( function( indx ) { $cells = $cells.filter( function( indx ) {
@ -62,33 +61,38 @@
}, },
// get all of the column numerical values in an arry // get all of the column numerical values in an arry
getColumn : function( c, $el, type ) { getColumn : function( c, $el, type, hasFilter ) {
var index, $t, $tr, len, $mathRows, mathAbove, var index, $t, $tr, len, $mathRows, mathAbove,
wo = c.widgetOptions, wo = c.widgetOptions,
arry = [], arry = [],
$row = $el.closest( 'tr' ), $row = $el.closest( 'tr' ),
mathAttr = wo.math_dataAttrib, mathAttr = wo.math_dataAttrib,
hasFilter = $row.attr( mathAttr + '-filter' ) || wo.math_rowFilter, mathIgnore = '[' + mathAttr + '=ignore]',
filtered = wo.filter_filteredRow || 'filtered', filtered = wo.filter_filteredRow || 'filtered',
cIndex = math.getCellIndex( $el ), cIndex = math.getCellIndex( $el ),
$rows = c.$table.children( 'tbody' ).children(); // get all rows to keep row indexing
// make sure tfoot rows are AFTER the tbody rows $rows = c.$table.children( 'tbody' ).children(),
// $rows.add( c.$table.children( 'tfoot' ).children() ); mathAttrs = [
'[' + mathAttr + '^=above]',
'[' + mathAttr + '^=below]',
'[' + mathAttr + '^=col]',
'[' + mathAttr + '^=all]'
];
if ( type === 'above' ) { if ( type === 'above' ) {
len = $rows.index( $row ); len = $rows.index( $row );
index = len; index = len;
while ( index >= 0 ) { while ( index >= 0 ) {
$tr = $rows.eq( index ); $tr = $rows.eq( index );
mathAbove = $tr.children().filter( mathAttrs[0] ).length;
if ( hasFilter ) { if ( hasFilter ) {
$tr = $tr.filter( hasFilter ); $tr = $tr.filter( hasFilter );
} }
$t = $tr.children().filter( function( indx ) { $t = $tr.children().filter( function( indx ) {
return math.getCellIndex( $( this ) ) === cIndex; return math.getCellIndex( $( this ) ) === cIndex;
}); });
mathAbove = $t.filter( '[' + mathAttr + '^=above]' ).length;
// ignore filtered rows & rows with data-math="ignore" (and starting row) // ignore filtered rows & rows with data-math="ignore" (and starting row)
if ( ( ( !$tr.hasClass( filtered ) || hasFilter ) && if ( ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
$tr.not( '[' + mathAttr + '=ignore]' ).length && $tr.not( mathIgnore ).length &&
index !== len ) || index !== len ) ||
mathAbove && index !== len ) { mathAbove && index !== len ) {
// stop calculating 'above', when encountering another 'above' // stop calculating 'above', when encountering another 'above'
@ -105,24 +109,23 @@
// index + 1 to ignore starting node // index + 1 to ignore starting node
for ( index = $rows.index( $row ) + 1; index < len; index++ ) { for ( index = $rows.index( $row ) + 1; index < len; index++ ) {
$tr = $rows.eq( index ); $tr = $rows.eq( index );
if ( $tr.children().filter( mathAttrs[1] ).length ) {
break;
}
if ( hasFilter ) { if ( hasFilter ) {
$tr = $tr.filter( hasFilter ); $tr = $tr.filter( hasFilter );
} }
$t = $tr.children().filter( function( indx ) { $t = $tr.children().filter( function( indx ) {
return math.getCellIndex( $( this ) ) === cIndex; return math.getCellIndex( $( this ) ) === cIndex;
}); });
if ( $t.filter( '[' + mathAttr + '^=below]' ).length ) { if ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
break; $tr.not( mathIgnore ).length &&
} $t.length ) {
if ( ( !$tr.hasClass( filtered ) || hasFilter ) &&
$tr.not( '[' + mathAttr + '=ignore]' ).length &&
$t.length ) {
arry.push( math.processText( c, $t ) ); arry.push( math.processText( c, $t ) );
} }
} }
} else { } else {
$mathRows = $rows.not( '[' + mathAttr + '=ignore]' ); $mathRows = $rows.not( mathIgnore );
len = $mathRows.length; len = $mathRows.length;
for ( index = 0; index < len; index++ ) { for ( index = 0; index < len; index++ ) {
$tr = $mathRows.eq( index ); $tr = $mathRows.eq( index );
@ -132,8 +135,8 @@
$t = $tr.children().filter( function( indx ) { $t = $tr.children().filter( function( indx ) {
return math.getCellIndex( $( this ) ) === cIndex; return math.getCellIndex( $( this ) ) === cIndex;
}); });
if ( ( !$tr.hasClass( filtered ) || hasFilter ) && if ( ( hasFilter || !$tr.hasClass( filtered ) ) &&
$t.not( '[' + mathAttr + '^=above],[' + mathAttr + '^=below],[' + mathAttr + '^=col]' ).length && $t.not( mathAttrs.join( ',' ) ).length &&
!$t.is( $el ) ) { !$t.is( $el ) ) {
arry.push( math.processText( c, $t ) ); arry.push( math.processText( c, $t ) );
} }
@ -143,22 +146,22 @@
}, },
// get all of the column numerical values in an arry // get all of the column numerical values in an arry
getAll : function( c ) { getAll : function( c, hasFilter ) {
var $t, col, $row, rowIndex, rowLen, $cells, cellIndex, cellLen, var $t, col, $row, rowIndex, rowLen, $cells, cellIndex, cellLen,
arry = [], arry = [],
wo = c.widgetOptions, wo = c.widgetOptions,
mathAttr = wo.math_dataAttrib, mathAttr = wo.math_dataAttrib,
mathIgnore = '[' + mathAttr + '=ignore]',
filtered = wo.filter_filteredRow || 'filtered', filtered = wo.filter_filteredRow || 'filtered',
hasFilter = wo.filter_rowFilter, $rows = c.$table.children( 'tbody' ).children().not( mathIgnore );
$rows = c.$table.children( 'tbody' ).children().not( '[' + mathAttr + '=ignore]' );
rowLen = $rows.length; rowLen = $rows.length;
for ( rowIndex = 0; rowIndex < rowLen; rowIndex++ ) { for ( rowIndex = 0; rowIndex < rowLen; rowIndex++ ) {
$row = $rows.eq( rowIndex ); $row = $rows.eq( rowIndex );
if ( hasFilter ) { if ( hasFilter ) {
$row = $row.filter( hasFilter ); $row = $row.filter( hasFilter );
} }
if ( !$row.hasClass( filtered ) || hasFilter ) { if ( hasFilter || !$row.hasClass( filtered ) ) {
$cells = $row.children().not( '[' + mathAttr + '=ignore]' ); $cells = $row.children().not( mathIgnore );
cellLen = $cells.length; cellLen = $cells.length;
// $row.children().each(function(){ // $row.children().each(function(){
for ( cellIndex = 0; cellIndex < cellLen; cellIndex++ ) { for ( cellIndex = 0; cellIndex < cellLen; cellIndex++ ) {
@ -215,7 +218,9 @@
recalculate : function(c, wo, init) { recalculate : function(c, wo, init) {
if ( c && ( !wo.math_isUpdating || init ) ) { if ( c && ( !wo.math_isUpdating || init ) ) {
var undef, time, mathAttr, $mathCells; var undef, time, mathAttr, $mathCells, indx, len,
changed = false,
filters = {};
if ( c.debug ) { if ( c.debug ) {
time = new Date(); time = new Date();
} }
@ -231,7 +236,7 @@
// all non-info tbody cells // all non-info tbody cells
mathAttr = wo.math_dataAttrib; mathAttr = wo.math_dataAttrib;
$mathCells = c.$tbodies.children( 'tr' ).children( '[' + mathAttr + ']' ); $mathCells = c.$tbodies.children( 'tr' ).children( '[' + mathAttr + ']' );
math.mathType( c, $mathCells, wo.math_priority ); changed = changed || math.mathType( c, $mathCells, wo.math_priority );
// only info tbody cells // only info tbody cells
$mathCells = c.$table $mathCells = c.$table
@ -242,20 +247,33 @@
// find the 'all' total // find the 'all' total
$mathCells = c.$table.children().children( 'tr' ).children( '[' + mathAttr + '^=all]' ); $mathCells = c.$table.children().children( 'tr' ).children( '[' + mathAttr + '^=all]' );
math.mathType( c, $mathCells, [ 'all' ] ); len = $mathCells.length;
// get math filter, if any
wo.math_isUpdating = true; // hasFilter = $row.attr( mathAttr + '-filter' ) || wo.math_rowFilter;
if ( c.debug ) { $mathCells.each( function( indx, cell ) {
console[ console.group ? 'group' : 'log' ]( 'Math widget triggering an update after recalculation' ); var $cell = $( cell ),
} filter = $mathCells.eq( indx ).attr( mathAttr + '-filter' ) || wo.math_rowFilter;
filters[ filter ] = filters[ filter ] ? filters[ filter ].add( $cell ) : $cell;
// update internal cache });
ts.update( c, undef, function(){ $.each( filters, function( hasFilter, $cells ) {
math.updateComplete( c ); changed = changed || math.mathType( c, $cells, [ 'all' ], hasFilter );
}); });
if ( c.debug ) { // trigger an update only if cells inside the tbody changed
console.log( 'Math widget update completed' + ts.benchmark( time ) ); if ( changed ) {
wo.math_isUpdating = true;
if ( c.debug ) {
console[ console.group ? 'group' : 'log' ]( 'Math widget triggering an update after recalculation' );
}
// update internal cache
ts.update( c, undef, function(){
math.updateComplete( c );
});
if ( c.debug ) {
console.log( 'Math widget update completed' + ts.benchmark( time ) );
}
} }
} }
}, },
@ -266,23 +284,25 @@
wo.math_isUpdating = false; wo.math_isUpdating = false;
}, },
mathType : function( c, $cells, priority ) { mathType : function( c, $cells, priority, hasFilter ) {
if ( $cells.length ) { if ( $cells.length ) {
var formula, result, $el, arry, getAll, $targetCells, index, len, var getAll,
changed = false,
wo = c.widgetOptions, wo = c.widgetOptions,
mathAttr = wo.math_dataAttrib, mathAttr = wo.math_dataAttrib,
equations = ts.equations; equations = ts.equations;
if ( priority[0] === 'all' ) { if ( priority[0] === 'all' ) {
// no need to get all cells more than once // mathType is called multiple times if more than one "hasFilter" is used
getAll = math.getAll( c ); getAll = math.getAll( c, hasFilter );
} }
if (c.debug) { if (c.debug) {
console[ console.group ? 'group' : 'log' ]( 'Tablesorter Math widget recalculation' ); console[ console.group ? 'group' : 'log' ]( 'Tablesorter Math widget recalculation' );
} }
// $.each is okay here... only 4 priorities // $.each is okay here... only 4 priorities
$.each( priority, function( i, type ) { $.each( priority, function( i, type ) {
$targetCells = $cells.filter( '[' + mathAttr + '^=' + type + ']' ); var index, arry, formula, result, $el,
len = $targetCells.length; $targetCells = $cells.filter( '[' + mathAttr + '^=' + type + ']' ),
len = $targetCells.length;
if ( len ) { if ( len ) {
if (c.debug) { if (c.debug) {
console[ console.group ? 'group' : 'log' ]( type ); console[ console.group ? 'group' : 'log' ]( type );
@ -293,39 +313,58 @@
if ( $el.parent().hasClass( wo.filter_filteredRow || 'filtered' ) ) { if ( $el.parent().hasClass( wo.filter_filteredRow || 'filtered' ) ) {
continue; continue;
} }
hasFilter = hasFilter || $el.attr( mathAttr + '-filter' ) || wo.math_rowFilter;
formula = ( $el.attr( mathAttr ) || '' ).replace( type + '-', '' ); formula = ( $el.attr( mathAttr ) || '' ).replace( type + '-', '' );
arry = ( type === 'row' ) ? math.getRow( c, $el ) : arry = ( type === 'row' ) ? math.getRow( c, $el, hasFilter ) :
( type === 'all' ) ? getAll : math.getColumn( c, $el, type ); ( type === 'all' ) ? getAll : math.getColumn( c, $el, type, hasFilter );
if ( equations[ formula ] ) { if ( equations[ formula ] ) {
if ( arry.length ) { if ( arry.length ) {
result = equations[ formula ]( arry, c ); result = equations[ formula ]( arry, c );
if ( c.debug ) { if ( c.debug ) {
console.log( $el.attr( mathAttr ), arry, '=', result ); console.log( $el.attr( mathAttr ), hasFilter ? '("' + hasFilter + '")' : '', arry, '=', result );
} }
} else { } else {
// mean will return a divide by zero error, everything else shows an undefined error // mean will return a divide by zero error, everything else shows an undefined error
result = math.invalid( c, formula, formula === 'mean' ? 0 : 'undef' ); result = math.invalid( c, formula, formula === 'mean' ? 0 : 'undef' );
} }
math.output( $el, wo, result, arry ); changed = math.output( $el, c, result, arry ) || changed;
} }
} }
if ( c.debug && console.groupEnd ) { console.groupEnd(); } if ( c.debug && console.groupEnd ) { console.groupEnd(); }
} }
}); });
if ( c.debug && console.groupEnd ) { console.groupEnd(); } if ( c.debug && console.groupEnd ) { console.groupEnd(); }
return changed;
} }
return false;
}, },
output : function( $cell, wo, value, arry ) { output : function( $cell, c, value, arry ) {
// get mask from cell data-attribute: data-math-mask="#,##0.00" // get mask from cell data-attribute: data-math-mask="#,##0.00"
var mask = $cell.attr( 'data-' + wo.math_data + '-mask' ) || wo.math_mask, var $el,
wo = c.widgetOptions,
changed = false,
prev = $cell.html(),
mask = $cell.attr( 'data-' + wo.math_data + '-mask' ) || wo.math_mask,
result = ts.formatMask( mask, value, wo.math_wrapPrefix, wo.math_wrapSuffix ); result = ts.formatMask( mask, value, wo.math_wrapPrefix, wo.math_wrapSuffix );
if ( typeof wo.math_complete === 'function' ) { if ( typeof wo.math_complete === 'function' ) {
result = wo.math_complete( $cell, wo, result, value, arry ); result = wo.math_complete( $cell, wo, result, value, arry );
} }
if ( result !== false ) { if ( result !== false ) {
changed = prev !== result;
$cell.html( result ); $cell.html( result );
} }
// check if in a regular tbody, otherwise don't pass a changed flag
// to prevent unnecessary updating of the table cache
if ( changed ) {
$el = $cell.closest( 'tbody' );
// content was changed in a tfoot, info-only tbody or the resulting tbody is in a nested table
// then don't signal a change
if ( !$el.length || $el.hasClass( c.cssInfoBlock ) || $el.parent()[0] !== c.table ) {
return false;
}
}
return changed;
} }
}; };
@ -560,10 +599,13 @@
}, },
init : function( table, thisWidget, c, wo ) { init : function( table, thisWidget, c, wo ) {
// filterEnd fires after updateComplete // filterEnd fires after updateComplete
var update = ts.hasWidget( table, 'filter' ) ? 'filterEnd' : 'updateComplete'; var update = ( ts.hasWidget( table, 'filter' ) ? 'filterEnd' : 'updateComplete' ) + '.tsmath';
// filterEnd is when the pager hides rows... so bind to pagerComplete
math.events += ( ts.hasWidget( table, 'pager' ) ? 'pagerComplete' : 'filterEnd' ) + '.tsmath ';
c.$table c.$table
.off( ( math.events + ' updateComplete.tsmath ' + wo.math_event ).replace( /\s+/g, ' ' ) ) .off( ( math.events + 'updateComplete.tsmath ' + wo.math_event ).replace( /\s+/g, ' ' ) )
.on( math.events + ' ' + wo.math_event, function( e ) { .on( math.events + wo.math_event, function( e ) {
if ( !this.hasInitialized ) { return; }
var init = e.type === 'tablesorter-initialized'; var init = e.type === 'tablesorter-initialized';
if ( !wo.math_isUpdating || init ) { if ( !wo.math_isUpdating || init ) {
// don't setColumnIndexes on init here, or it gets done twice // don't setColumnIndexes on init here, or it gets done twice
@ -574,7 +616,7 @@
math.recalculate( c, wo, init ); math.recalculate( c, wo, init );
} }
}) })
.on( update + '.tsmath', function() { .on( update, function() {
setTimeout( function(){ setTimeout( function(){
math.updateComplete( c ); math.updateComplete( c );
}, 40 ); }, 40 );