Math: values are now obtained from data-attributes first

This commit is contained in:
Mottie 2014-05-23 07:48:23 -05:00
parent 876e49bf1a
commit ff46cccb9d
2 changed files with 28 additions and 8 deletions

View File

@ -181,6 +181,12 @@
<h3><a href="#">Notes</a></h3> <h3><a href="#">Notes</a></h3>
<div> <div>
<ul> <ul>
<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: <li>In <span class="version">v2.16.4</span>, added:
<ul> <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>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>
@ -513,7 +519,7 @@ BAD => No minus (-) here! $#,###.00 or [-] here either <= BAD</textarea>
</tr> </tr>
<tr> <tr>
<th colspan="5">Grand Total</th> <th colspan="5">Grand Total</th>
<th data-math="all-sum" data-math-mask="##0.00">all-sum</th> <th data-math="all-sum" data-math-mask="##0.0000">all-sum</th>
</tr> </tr>
</tfoot> </tfoot>
@ -536,8 +542,8 @@ BAD => No minus (-) here! $#,###.00 or [-] here either <= BAD</textarea>
<tbody> <tbody>
<tr><th>North</th><td>Joseph</td><td>$ 3,643</td><td>$ 5,846</td><td>$ 6,574</td><td class="totals" data-math="row-sum">row-sum</td></tr> <tr><th>North</th><td>Joseph</td><td>$ 3,643</td><td>$ 5,846</td><td>$ 6,574</td><td class="totals" data-math="row-sum">row-sum</td></tr>
<tr><th>North</th><td>Lawrence</td><td>$ 4,456</td><td>$ 6,658</td><td>$ 7,685</td><td class="totals" data-math="row-sum">row-sum</td></tr> <tr><th>North</th><td>Lawrence</td><td>$ 4,456</td><td>$ 6,658</td><td data-text="$ 7,685.0049">$ 7,685</td><td class="totals" data-math="row-sum">row-sum</td></tr>
<tr><th>North</th><td>Maria</td><td>$ 6,235</td><td>$ 4,616.99</td><td>$ 3,612.33</td><td class="totals" data-math="row-sum">row-sum</td></tr> <tr><th>North</th><td>Maria</td><td>$ 6,235</td><td>$ 4,616.99</td><td data-text="3612.3267">$ 3,612.33</td><td class="totals" data-math="row-sum">row-sum</td></tr>
<tr><th>North</th><td>Matt</td><td>$ 3,868</td><td>$ 3,926</td><td>$ 3,254</td><td class="totals" data-math="row-sum">row-sum</td></tr> <tr><th>North</th><td>Matt</td><td>$ 3,868</td><td>$ 3,926</td><td>$ 3,254</td><td class="totals" data-math="row-sum">row-sum</td></tr>
</tbody> </tbody>

View File

@ -12,7 +12,8 @@
// get all of the row numerical values in an arry // get all of the row numerical values in an arry
getRow : function(table, wo, $el, dataAttrib) { getRow : function(table, wo, $el, dataAttrib) {
var txt, var $t, txt,
c = table.config,
arry = [], arry = [],
$row = $el.closest('tr'), $row = $el.closest('tr'),
$cells = $row.children(); $cells = $row.children();
@ -21,7 +22,11 @@
$cells = $cells.not('[' + dataAttrib + '=ignore]').not('[data-column=' + wo.math_ignore.join('],[data-column=') + ']'); $cells = $cells.not('[' + dataAttrib + '=ignore]').not('[data-column=' + wo.math_ignore.join('],[data-column=') + ']');
} }
arry = $cells.not($el).map(function(){ arry = $cells.not($el).map(function(){
txt = this.textContent || $(this).text(); $t = $(this);
txt = $t.attr(c.textAttribute);
if (typeof txt === "undefined") {
txt = this.textContent || $t.text();
}
txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table); txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table);
return isNaN(txt) ? 0 : txt; return isNaN(txt) ? 0 : txt;
}).get(); }).get();
@ -52,7 +57,10 @@
if (mathAbove) { if (mathAbove) {
i = 0; i = 0;
} else if ($t.length) { } else if ($t.length) {
txt = $t[0].textContent || $t.text(); txt = $t.attr(c.textAttribute);
if (typeof txt === "undefined") {
txt = $t[0].textContent || $t.text();
}
txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table); txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table);
arry.push(isNaN(txt) ? 0 : txt); arry.push(isNaN(txt) ? 0 : txt);
} }
@ -63,7 +71,10 @@
$rows.each(function(){ $rows.each(function(){
$t = $(this).children().filter('[data-column=' + cIndex + ']'); $t = $(this).children().filter('[data-column=' + cIndex + ']');
if (!$(this).hasClass(filtered) && $t.not('[' + dataAttrib + '^=above],[' + dataAttrib + '^=col]').length && !$t.is($el)) { if (!$(this).hasClass(filtered) && $t.not('[' + dataAttrib + '^=above],[' + dataAttrib + '^=col]').length && !$t.is($el)) {
txt = ($t[0] ? $t[0].textContent : '') || $t.text(); txt = $t.attr(c.textAttribute);
if (typeof txt === "undefined") {
txt = ($t[0] ? $t[0].textContent : '') || $t.text();
}
txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table); txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table);
arry.push(isNaN(txt) ? 0 : txt); arry.push(isNaN(txt) ? 0 : txt);
} }
@ -85,7 +96,10 @@
$t = $(this); $t = $(this);
col = parseInt( $t.attr('data-column'), 10); col = parseInt( $t.attr('data-column'), 10);
if (!$t.filter('[' + dataAttrib + ']').length && $.inArray(col, wo.math_ignore) < 0) { if (!$t.filter('[' + dataAttrib + ']').length && $.inArray(col, wo.math_ignore) < 0) {
txt = ($t[0] ? $t[0].textContent : '') || $t.text(); txt = $t.attr(c.textAttribute);
if (typeof txt === "undefined") {
txt = ($t[0] ? $t[0].textContent : '') || $t.text();
}
txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table); txt = ts.formatFloat(txt.replace(/[^\w,. \-()]/g, ""), table);
arry.push(isNaN(txt) ? 0 : txt); arry.push(isNaN(txt) ? 0 : txt);
} }