Collect sum of a column in a html table?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2007
Posts: 253
Reputation: Designer_101 is an unknown quantity at this point 
Solved Threads: 12
Designer_101's Avatar
Designer_101 Designer_101 is offline Offline
Posting Whiz in Training

Collect sum of a column in a html table?

 
0
  #1
Apr 18th, 2009
Hi

This is either really simple or completely impossible

I have a table like the following:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <table>
  2. <tr>
  3. <td>1</td>
  4. <td>2</td>
  5. </tr>
  6.  
  7. <tr>
  8. <td>7</td>
  9. <td>9</td>
  10. </tr>
  11.  
  12. <tr>
  13. <td><<SUM OF THIS COLUMN (8)>></td>
  14. <td><<SUM OF THIS COLUMN(11)>></td>
  15. </tr>
  16. </table>

I hope you understand what it is im asking. This is easy to do in mysql however can it be done in standard html/dhtml ?

Thanks
Did I help? Add to my reputation I need it :(
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 439
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Collect sum of a column in a html table?

 
0
  #2
Apr 18th, 2009
you would do the following. Hope it helps.

  1. <html>
  2. <head>
  3. </head>
  4. <body>
  5. <table id="countit">
  6. <tr>
  7. <td class="count-me">12</td>
  8. <td>Some value</td>
  9. </tr>
  10. <tr>
  11. <td class="count-me">2</td>
  12. <td>Some value</td>
  13. </tr>
  14. <tr>
  15. <td class="count-me">17</td>
  16. <td>Some value</td>
  17. </tr>
  18. </table>
  19. <script language="javascript" type="text/javascript">
  20. var tds = document.getElementById('countit').getElementsByTagName('td');
  21. var sum = 0;
  22. for(var i = 0; i < tds.length; i ++) {
  23. if(tds[i].className == 'count-me') {
  24. sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
  25. }
  26. }
  27. document.getElementById('countit').innerHTML += '<tr> <td> ' + sum + '</td> <td> total</td> </tr> ';
  28. </script>
  29. </body>
  30. </html>
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 884
Reputation: Airshow will become famous soon enough Airshow will become famous soon enough 
Solved Threads: 126
Airshow's Avatar
Airshow Airshow is offline Offline
Practically a Posting Shark

Re: Collect sum of a column in a html table?

 
0
  #3
Apr 18th, 2009
101,

The solution depends on why you need to do this?

If you build your table manually, then do the summation manually and type in the HTML for the totals row manually, complete with its values.

If your HTML is pasted from a spreadsheed, then get the spreadsheet to do the totals and paste in the total rows along with the other rows.

If you build the table dynamically, either server-side or client-side, then get the build script to do the summation and write the totals row as per the other rows.

If the values in the table occur in response to some sort of user interaction(s), then OK, use JavaScript to scan the table and write in the totals but that would require the calculation script to be triggered by the user event(s) in question (data entry/button press or whatever) rather than using a "one-off" script embedded in the HTML. Write the code as a javascript function and call it from event handler(s) as appropriate.

Also (from solution above), my IE6 (maybe other browsers) doesn't like document.getElementById('countit').innerHTML += '.....'; . It may work in Moz/Opera, I haven't tested. For me, it's better to add <tr><td id="total_1"></td><td id="total_2"></td></tr> to the bottom of the table (manually if that's how you write your HTML), then populate it from the calculation function. eg something like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.getElementById('total_1').innerHTML = sum1;
  2. document.getElementById('total_2').innerHTML = sum2;
This has the added advantage of allowing reuse; the function can be called more than once (per page-session) without adding a growing number total rows to the table, which is important if the purpose of all this is to respond to user events (in the plural).

Apart from that, and with my proviso about reuse, Fungus' script will work if a one-off, in-line calculation is what's required.

Hope this helps

Airshow
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC