Note that there are some explanatory texts on larger screens.

plurals
  1. POcalculating column totals in multiple tables using
    primarykey
    data
    text
    <p>I'm a relative newb with jQuery but I've managed in the past to cobble together a few simple scripts. I've got a new challenge and I know I'm 'in the zone but I need some help.</p> <p>I have an html page, with numerous tables in this format:</p> <pre><code>&lt;table class="tableClass" id="tableID"&gt; &lt;col class="date" /&gt; &lt;col class="reference" /&gt; &lt;col class="amount" /&gt; &lt;thead&gt; &lt;tr&gt; &lt;th class="date"&gt;Date&lt;/th&gt; &lt;th class="reference"&gt;Reference&lt;/th&gt; &lt;th class="amount"&gt;Amount&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;01-11-09&lt;/td&gt; &lt;td&gt;text here&lt;/td&gt; &lt;td&gt;33.66&lt;/td&gt; &lt;/tr&gt; &lt;!— [ etc., many more rows ] --&gt; &lt;tr class="total"&gt; &lt;td colspan="2"&gt;TOTAL:&lt;/td&gt; &lt;td class="amount total"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I'm using this bit of jQuery to add the class="amount" to the third cell:</p> <pre><code> &lt;script type="text/javascript"&gt; $(document).ready(function(){ $("tbody tr td:nth-child(3)").addClass("amount").append("&lt;span&gt;$&lt;\/span&gt;"); }); &lt;/script&gt; </code></pre> <p>... which works as intended.</p> <p>My objective is to have jQuery calculate <em>in each of multiple tables</em> the total of the 'amount' cells, and display the result in the designated cells (tr.total td.total). With help from a non-jQuerying javascripter, I've cobbled this together:</p> <pre><code>// instantiate the 'total' variable var total = 0; var currTotal = 0.00; $(document).ready(function(){ $('table').each(function(){ // iterate through 3rd cell in each row in the tbody (td with an amount): $("#tableID tbody tr td:nth-child(3)").css("color", "red").each(function() { total += parseInt(100 * parseFloat($(this).text())); }); currTotal = total/100; alert('Total = $ ' + currTotal); $(this).parent().find(".total .amount").html('&lt;span&gt;$&lt;\/span&gt;' + currTotal); }); }); </code></pre> <p>This (apparently) totals all 'amount' cells in the page, and writes it in <em>all</em> the 'total' cells - close but obviously I'm not correctly specifying that each total should be displayed in its parent. I'd greatly appreciate it if anyone can please straighten me out on what I'm missing, and if there's a simpler way to achieve the rest of it, I'm all ears.</p> <p>Cheers, svs</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload