Math: Add math_textAttr. See #1601

This commit is contained in:
Rob Garrison 2018-11-20 15:45:31 -06:00
parent 35c3bef31e
commit 3d4e5cbbaf
3 changed files with 28 additions and 3 deletions

View File

@ -114,6 +114,8 @@
// called after all math calculations have completed
console.log( 'math calculations complete', c.$table.find('[data-math="all-sum"]:first').text() );
},
// cell data-attribute containing the math value to use (added v2.31.1)
math_textAttr : '',
// see "Mask Examples" section
math_mask : '#,##0.00',
math_prefix : '', // custom string added before the math_mask value (usually HTML)
@ -196,6 +198,7 @@
<h3><a href="#">Notes</a></h3>
<div>
<ul>
<li>In <span class="version">v2.31.1</span>, added <a href="#math_textattr" class="intlink"><code>math_textAttr</code></a>.</li>
<li>In <span class="version">v2.28.0</span>
<ul>
<li>Fix documentation and processing of <code>math_prefix</code> and <code>math_suffix</code> options. See <a href="https://github.com/Mottie/tablesorter/issues/1305">issue #1305</a>.</li>
@ -305,6 +308,22 @@
</td>
</tr>
<tr id="math_textattr">
<td><a href="#" class="permalink">math_textAttr</a></td>
<td><code>''</code></td>
<td>
Cell attribute (data-attribute) containing the math value to use. This defaults to the <a href="index.html#textattribute"><code>textAttribute</code></a> value when the <a href="index.html#textextraction"><code>textExtraction</code></a> option is set to "basic" (<span class="version">v2.31.1</span>).
<div class="collapsible">
<br>
Use this option as follows:
<pre class="prettyprint lang-js">// Include the "data-" prefix in the option
math_textAttr : 'data-math-value'</pre>
<pre class="prettyprint lang-html">&lt;td data-math-value="1000"&gt;1.0k&lt;/td&gt;</pre>
</div>
</td>
</tr>
<tr id="math_event">
<td><a href="#" class="permalink">math_event</a></td>
<td><code>'recalculate'</code></td>

View File

@ -495,7 +495,7 @@
<li><a href="example-widget-header-titles.html">Header titles widget</a> (v2.15.6; <span class="version updated">v2.24.4</span>).</li>
<li><span class="label label-info">Beta</span> <a href="example-widget-lazyload.html">Lazyload widget</a> (<span class="version">v2.24.0</span>; <span class="version updated">v2.25.7</span>).</li>
<li><a href="example-widget-mark.html">Mark widget</a> (<span class="version">v2.27.6</span>).</li>
<li><a href="example-widget-math.html">Math widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.28.0</span>).</li>
<li><a href="example-widget-math.html">Math widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.31.1</span>).</li>
<li>
<a href="example-widget-output.html">Output widget</a> (<span class="version">v2.16</span>; <span class="version updated">v2.29.0</span>).
<br><br>

View File

@ -1,4 +1,4 @@
/*! Widget: math - updated 5/3/2017 (v2.28.9) *//*
/*! Widget: math - updated 11/20/2018 (v2.31.1) *//*
* Requires tablesorter v2.16+ and jQuery 1.7+
* by Rob Garrison
*/
@ -28,10 +28,14 @@
events : ( 'tablesorter-initialized update updateAll updateRows addRows updateCell filterReset ' )
.split(' ').join('.tsmath '),
processText : function( c, $cell ) {
processText : function( c, $cell ) {
var tmp,
wo = c.widgetOptions,
txt = ts.getElementText( c, $cell, math.getCellIndex( $cell ) ),
prefix = c.widgetOptions.math_prefix;
if (wo.math_textAttr) {
txt = $cell.attr(wo.math_textAttr) || txt;
}
if ( /</.test( prefix ) ) {
// prefix contains HTML; remove it & any text before using formatFloat
tmp = $( '<div>' + prefix + '</div>' ).text()
@ -612,6 +616,8 @@
// e.g. '<span class="red">{content}</span>'
math_prefix : '',
math_suffix : '',
// cell attribute containing the math value to use
math_textAttr : '',
// no matching math elements found (text added to cell)
math_none : 'N/A',
math_event : 'recalculate',