943,584 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Apr 18th, 2009
0

Collect sum of a column in a html table?

Expand Post »
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
Reputation Points: 12
Solved Threads: 16
Posting Whiz
Designer_101 is offline Offline
314 posts
since Jul 2007
Apr 18th, 2009
0

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

you would do the following. Hope it helps.

html Syntax (Toggle Plain Text)
  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>
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Apr 18th, 2009
0

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

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
Sponsor
Reputation Points: 300
Solved Threads: 357
WiFi Lounge Lizard
Airshow is offline Offline
2,522 posts
since Apr 2009
Jan 4th, 2010
0
Re: Collect sum of a column in a html table?
Thanks for this code. We have used it to calculate Donations to a Public Radio Station in WA, but we have additional rows that we want to ignore (the Check Number). How would that work? Here is the code...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <table id="cbtf_24">
  2. <tr>
  3. <td class="titleCell"><label for="68">First Donation $:</label></td>
  4. <td class="fieldCell" id="68">2</td>
  5.  
  6. </tr>
  7. <tr>
  8. <td class="titleCell"><label for="69">First Donation Date:</label></td>
  9. <td class="fieldCell" id="69">01/01/2010</td>
  10. </tr>
  11. <tr>
  12. <td class="titleCell"><label for="cbfv_74">First Check Number:</label></td>
  13. <td class="fieldCell" id="74">101010</td>
  14. </tr>
  15. <tr>
  16. <td class="titleCell"><label for="75">2nd Donation $:</label></td>
  17. <td class="fieldCell" id="75">1</td>
  18. </tr>
  19. <tr>
  20.  
  21. <td class="titleCell"><label for="76">2nd Donation Date:</label></td>
  22. <td class="fieldCell" id="76">-</td>
  23. </tr>
  24. <tr>
  25. <td class="titleCell"><label for="77">2nd Check Number:</label></td>
  26. <td class="fieldCell" id="77">-</td>
  27. </tr>
  28.  
  29. <tr>
  30. <td class="titleCell"><label for="78">3rd Donation $:</label></td>
  31. <td class="fieldCell" id="78">4</td>
  32. </tr>
  33. <tr>
  34. <td class="titleCell"><label for="79">3rd Donation Date:</label></td>
  35. <td class="fieldCell" id="79">-</td>
  36.  
  37. </tr>
  38. <tr>
  39. <td class="titleCell"><label for="80">3rd Check Number:</label></td>
  40. <td class="fieldCell" id="80">-</td>
  41. </tr>
  42. <tr>
  43. <td class="titleCell"><label for="81">4th Donation $:</label></td>
  44.  
  45. <td class="fieldCell" id="81">3</td>
  46. </tr>
  47. <tr>
  48. <td class="titleCell"><label for="82">4th Donation Date:</label></td>
  49. <td class="fieldCell" id="82">-</td>
  50. </tr>
  51. <tr>
  52.  
  53. <td class="titleCell"><label for="83">4th Check Number:</label></td>
  54. <td class="fieldCell" id="83">-</td>
  55. </tr>
  56. <tr>
  57. <td class="titleCell"><label for="84">5th Donation $:</label></td>
  58. <td class="fieldCell" id="84">99</td>
  59. </tr>
  60.  
  61. <tr>
  62. <td class="titleCell"><label for="85">5th Donation Date:</label></td>
  63. <td class="fieldCell" id="85">-</td>
  64. </tr>
  65. <tr>
  66. <td class="titleCell"><label for="86">5th Check Number:</label></td>
  67. <td class="fieldCell" id="86">-</td>
  68.  
  69. </tr>
  70. </table>
  71.  
  72. <script language="javascript" type="text/javascript">
  73.  
  74. var tds = document.getElementById('cbtf_24').getElementsByTagName('td');
  75. var sum = 0;
  76. for(var i = 0; i < tds.length; i ++) {
  77. if(tds[i].className == 'fieldCell') {
  78. sum += isNaN(tds[i].innerHTML) ? 0 : parseInt(tds[i].innerHTML);
  79. }
  80. }
  81. document.getElementById('cbtf_24').innerHTML += '<tr> <td> ' + sum + '</td> <td> total</td> </tr> ';
  82. </script>
As you can tell, the Check Number would be included in the sum, which is obviously not intended. Please keep in mind that the id and class names are created by a content management system and cannot be changed. Is there a way to only parse td's where the previous td contains the text "Donation $" ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
equality4xy is offline Offline
4 posts
since Jan 2010
Jan 4th, 2010
0
Re: Collect sum of a column in a html table?
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script>
  2.  
  3. function totalTable() {
  4. var arr = document.getElementsByTagName("TD");
  5. var count1 = 0;
  6. var count2 = 0;
  7. for (a = 0; a < arr.length; a++){
  8. var mytd = arr[a].innerHTML;
  9. if (isNumeric(mytd, false) == true) {
  10. if (arr[a].className == "column1"){
  11. count1 = count1 + parseInt(mytd);
  12. } else {
  13. count2 = count2 + parseInt(mytd);
  14. }
  15. }
  16. }
  17. document.getElementById("total1").innerHTML = "SUM OF THIS COLUMN (" + count1 + ")";
  18. document.getElementById("total2").innerHTML = "SUM OF THIS COLUMN (" + count2 + ")";
  19. //alert(count1 + " " + count2);
  20. }
  21. function isNumeric(sText, decimalAllowed) {
  22. if (sText.length == 0) return false;
  23. var validChars = "";
  24. if (decimalAllowed) {
  25. validChars = "0123456789.";
  26. } else {
  27. validChars = "0123456789";
  28. }
  29. var isNumber = true;
  30. var charA;
  31. var decimalCount = 0;
  32. for (i = 0; i < sText.length && isNumber == true && decimalCount < 2; i++) {
  33. charA = sText.charAt(i);
  34. if (charA == ".") {
  35. decimalCount += 1;
  36. }
  37. if (validChars.indexOf(charA) == -1) {
  38. isNumber = false;
  39. }
  40. }
  41. return isNumber;
  42. }
  43. window.onload = totalTable;
  44. </script>
Reputation Points: 10
Solved Threads: 13
Junior Poster in Training
harrierdh is offline Offline
62 posts
since Dec 2009
Jan 4th, 2010
0
Re: Collect sum of a column in a html table?
harrierdh,

Thanks for that but this is a single column output not two columns. Also I am unable to change the class or id parameters because it is part of a CMS.

I need to only parse the sum of certain td tags and not others that are in the same column.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
equality4xy is offline Offline
4 posts
since Jan 2010
Jan 5th, 2010
0
Re: Collect sum of a column in a html table?
Can you provide a little more info. Do a view source and put that in between [code] tags. Or print screen or link or something. It is hard to visualize what your table looks like with real data. What you are trying to do is completely possible with or without being able to modify id's or name's.
Reputation Points: 10
Solved Threads: 13
Junior Poster in Training
harrierdh is offline Offline
62 posts
since Dec 2009
Jan 6th, 2010
0
Re: Collect sum of a column in a html table?
Hi there, I agree with everyones comments including creating functions for re-usability yadda yadda yadda, BUT the kid asked how to total up a table using just DHTML.

Here is the revised code to not add the Check Number rows to the sum.

javascript Syntax (Toggle Plain Text)
  1. var trs = document.getElementById('cbtf_24').getElementsByTagName('tr');
  2. var sum = 0;
  3.  
  4. // Loop through each row
  5. for(var k = 0; k < trs.length; k ++) {
  6. var tds = trs[k].getElementsByTagName('td');
  7. var lbl = tds[0].getElementsByTagName('label')[0].innerHTML;
  8.  
  9. // Check the first column does not contain the word Check Number
  10. if(lbl.indexOf('Check Number') == -1) {
  11. sum += isNaN(tds[1].innerHTML) ? 0 : parseInt(tds[1].innerHTML);
  12. }
  13. }
  14.  
  15. document.getElementById('cbtf_24').innerHTML += '<tr> <td> ' + sum + '</td> <td> total</td> </tr> ';
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Jan 6th, 2010
0
Re: Collect sum of a column in a html table?
this is an alternate method I am trying (but not succeeding) that will give you a good idea what I intend. I need to sum only the rows that have "Donation $" in the 1st column (1st td of the tr). Can anyone tell what I have done wrong? It is not displaying the total at the bottom...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <table class="cbFieldsContentsTab cbFields" id="cbtf_24">
  2. <tr class="sectiontableentry2 cbft_integer" id="cbfr_68">
  3. <td class="titleCell"><label for="cbfv_68">First Donation $:</label></td>
  4. <td class="fieldCell" id="cbfv_68">2</td>
  5. </tr>
  6. <tr class="sectiontableentry1 cbft_date" id="cbfr_69">
  7. <td class="titleCell"><label for="cbfv_69">First Donation Date:</label></td>
  8.  
  9. <td class="fieldCell" id="cbfv_69">01/01/2010</td>
  10. </tr>
  11. <tr class="sectiontableentry2 cbft_text" id="cbfr_74">
  12. <td class="titleCell"><label for="cbfv_74">First Check Number:</label></td>
  13. <td class="fieldCell" id="cbfv_74">101010</td>
  14. </tr>
  15. <tr class="sectiontableentry1 cbft_integer" id="cbfr_75">
  16.  
  17. <td class="titleCell"><label for="cbfv_75">2nd Donation $:</label></td>
  18. <td class="fieldCell" id="cbfv_75">1</td>
  19. </tr>
  20. <tr class="sectiontableentry2 cbft_date" id="cbfr_76">
  21. <td class="titleCell"><label for="cbfv_76">2nd Donation Date:</label></td>
  22. <td class="fieldCell" id="cbfv_76">-</td>
  23. </tr>
  24.  
  25. <tr class="sectiontableentry1 cbft_text" id="cbfr_77">
  26. <td class="titleCell"><label for="cbfv_77">2nd Check Number:</label></td>
  27. <td class="fieldCell" id="cbfv_77">-</td>
  28. </tr>
  29. <tr class="sectiontableentry2 cbft_integer" id="cbfr_78">
  30. <td class="titleCell"><label for="cbfv_78">3rd Donation $:</label></td>
  31. <td class="fieldCell" id="cbfv_78">4</td>
  32.  
  33. </tr>
  34. <tr class="sectiontableentry1 cbft_date" id="cbfr_79">
  35. <td class="titleCell"><label for="cbfv_79">3rd Donation Date:</label></td>
  36. <td class="fieldCell" id="cbfv_79">-</td>
  37. </tr>
  38. <tr class="sectiontableentry2 cbft_text" id="cbfr_80">
  39. <td class="titleCell"><label for="cbfv_80">3rd Check Number:</label></td>
  40.  
  41. <td class="fieldCell" id="cbfv_80">-</td>
  42. </tr>
  43. <tr class="sectiontableentry1 cbft_integer" id="cbfr_81">
  44. <td class="titleCell"><label for="cbfv_81">4th Donation $:</label></td>
  45. <td class="fieldCell" id="cbfv_81">3</td>
  46. </tr>
  47. <tr class="sectiontableentry2 cbft_date" id="cbfr_82">
  48.  
  49. <td class="titleCell"><label for="cbfv_82">4th Donation Date:</label></td>
  50. <td class="fieldCell" id="cbfv_82">-</td>
  51. </tr>
  52. <tr class="sectiontableentry1 cbft_text" id="cbfr_83">
  53. <td class="titleCell"><label for="cbfv_83">4th Check Number:</label></td>
  54. <td class="fieldCell" id="cbfv_83">-</td>
  55. </tr>
  56.  
  57. <tr class="sectiontableentry2 cbft_integer" id="cbfr_84">
  58. <td class="titleCell"><label for="cbfv_84">5th Donation $:</label></td>
  59. <td class="fieldCell" id="cbfv_84">99</td>
  60. </tr>
  61. <tr class="sectiontableentry1 cbft_date" id="cbfr_85">
  62. <td class="titleCell"><label for="cbfv_85">5th Donation Date:</label></td>
  63. <td class="fieldCell" id="cbfv_85">-</td>
  64.  
  65. </tr>
  66. <tr class="sectiontableentry2 cbft_text" id="cbfr_86">
  67. <td class="titleCell"><label for="cbfv_86">5th Check Number:</label></td>
  68. <td class="fieldCell" id="cbfv_86">-</td>
  69. </tr>
  70. </table></div>
  71. </div></div></div><div class="cbClr"></div></div></div><div class="cbClr"></div></div>
  72. <div class="cbClr"></div>
  73. <div class="moduletable">
  74.  
  75. <script language="javascript" type="text/javascript">
  76.  
  77. var trs = document.getElementById('cbtf_24').getElementsByTagName('tr');
  78. var sum = 0;
  79. for(var i = 0; i < trs.length; i ++) {
  80. if(trs[i].childNode[1].className == 'fieldCell' && trs[i].childNode[0].childNode.innerHTML.match("Donation Date") == null && trs[i].childNode[0].childNode.innerHTML.match("Check Number") == null) ) {
  81. sum += isNaN(trs[i].childNode[1].innerHTML) ? 0 : parseInt(trs[i].childNode[1].innerHTML);
  82. }
  83. }
  84. document.getElementById('cbtf_24').innerHTML += '<tr> <td> Total </td> <td> ' + sum + ' </td> </tr>';
  85. </script>
Last edited by equality4xy; Jan 6th, 2010 at 1:25 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
equality4xy is offline Offline
4 posts
since Jan 2010
Jan 7th, 2010
1
Re: Collect sum of a column in a html table?
I have changed my original code to work only by calculating the sum of rows that have 'Donation $' in the first column.

javascript Syntax (Toggle Plain Text)
  1. var trs = document.getElementById('cbtf_24').getElementsByTagName('tr');
  2. var sum = 0;
  3. for(var i = 0; i < trs.length; i ++) {
  4. if(trs[i].childNode[1].className == 'fieldCell') {
  5. var lbl = trs[i].getElementsByTagName('label')[0].innerHTML;
  6. if(lbl.indexOf('Donation $') !== -1) {
  7. sum += isNaN(trs[i].childNode[1].innerHTML) ? 0 : parseInt(trs[i].childNode[1].innerHTML);
  8. }
  9. }
  10. }
  11. document.getElementById('cbtf_24').innerHTML += '<tr> <td> Total </td> <td> ' + sum + ' </td> </tr>';
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How do I make a website link not a pop up?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Form validation and No empty values with JS and JQuery





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC