Connecting Tech Pros Worldwide Help | Site Map

count the values of a table column

  #1  
Old August 27th, 2008, 04:27 PM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
Hi I have a table that I add rows to using javascript. I need to have a runnig total of one of the columns. Does anyone know how this can be done?

Expand|Select|Wrap|Line Numbers
  1. <table border="2" cellspacing="2" cellpadding="2" width=100% id="myTable1">
  2. <tr>
  3. <th> </th>
  4. <th>Part no.</th>
  5. <th>Description</th>
  6. <th>Price (£)</th>
  7. <th>Quantity</th>
  8. <th>Discount</th>
  9. <th>Supplier</th>
  10. <th>Sub Total</th>
  11. </tr>
  12. </table>
  13. <span>£<span id ="total"></span></span>
heres the javascript I use to add a row
Expand|Select|Wrap|Line Numbers
  1. var tbl = document.getElementById('myTable1');
  2.     var lastRow = tbl.rows.length;
  3.     // alert (lastRow);
  4.     // if there's no header row in the table, then iteration = lastRow + 1
  5.       var iteration = lastRow;
  6.       var row = tbl.insertRow(lastRow);
  7.  
  8.       // cell number
  9.       var cellLeft = row.insertCell(0);
  10.       var textNode = document.createTextNode(iteration);
  11.       cellLeft.appendChild(textNode);
  12.  
  13.       // part number
  14.       var cellpno = row.insertCell(1);
  15.       var t_pno = document.createTextNode(part_no);
  16.       cellpno.appendChild(t_pno);
  17.  
  18.       //description
  19.       var celldesc = row.insertCell(2);
  20.       var t_desc = document.createTextNode(description);
  21.       celldesc.appendChild(t_desc);
  22.  
  23.       //price
  24.       var cellprice = row.insertCell(3);
  25.       var t_price = document.createTextNode(price);
  26.       cellprice.appendChild(t_price);
  27.  
  28.       //quantity
  29.       var cellqty = row.insertCell(4);
  30.       var t_qty = document.createTextNode(quantity);
  31.       cellqty.appendChild(t_qty);
  32.  
  33.       //discount
  34.       var celldisc = row.insertCell(5);
  35.       var t_disc = document.createTextNode(discount);
  36.       celldisc.appendChild(t_disc);
  37.  
  38.       //supplier
  39.       var cellsup = row.insertCell(6);
  40.       var t_sup = document.createTextNode(supplier);
  41.       cellsup.appendChild(t_sup);
  42.  
  43.       //subtotal
  44.       var cellsub = row.insertCell(7);
  45.       var sub = quantity * price;
  46.       var sub1 = (sub / 100) * discount;
  47.       var sub2 = sub - sub1;
  48.       var result = sub2.toFixed(2); 
  49.       var t_sub = document.createTextNode(result);
  50.       cellsub.appendChild(t_sub);
  51.  
  52.       document.getElementById('part_no').value = "";
  53.       document.getElementById('quantity').value = "";
  54.       document.getElementById('description').value = "";
  55.       document.getElementById('price').value = "";
  56.       document.getElementById('discount').value = "";
  57.       total();
  #2  
Old August 27th, 2008, 10:15 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,490
Provided Answers: 10

re: count the values of a table column


I'm not sure if you want to count rows or columns, nevertheless try following:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("mytable1").childNodes.length 
  2. // should count tr elements (except when you use tbody)
  3. document.getElementById("mytable1").firstChild.childNodes.length
  4. // should count td elements of the first row
regards
  #3  
Old August 28th, 2008, 08:23 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164

re: count the values of a table column


Not sure what the first line is doing as it always gives a result of 1. The second line seems to count he number of rows. What I want to do is loop through each row and add the contents of a particular column to a variable. Any ideas?
  #4  
Old August 28th, 2008, 09:04 AM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

re: count the values of a table column


hi ...

try the following:

Expand|Select|Wrap|Line Numbers
  1. alert(document.getElementById("mytable1").childNodes[0].tagName);
i think this will tell you 'tbody' ... i guess you use IE? ... so you just need to add one step more and loop through the rows, then in every row though the coloumns and extract the value you need. for better advice it would be helpful to have something to work with ... so please post an example that you have so far.

kind regards
  #5  
Old August 28th, 2008, 09:17 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164

re: count the values of a table column


I havnt used tbody in my code. heres what I have so far

Expand|Select|Wrap|Line Numbers
  1. function total() {
  2.     var tbl = document.getElementById('myTable1');
  3.     var lastRow = tbl.rows.length;
  4.     for (var i=1; i < lastRow; i++) {
  5.         alert(tbl.rows[i].cols[7]);
  6.     }
  7. }
If I could just work out how to get the value of column7 for each row I would be sorted
  #6  
Old August 28th, 2008, 09:24 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164

re: count the values of a table column


ive worked it out

Expand|Select|Wrap|Line Numbers
  1. function total() {
  2.     var tbl = document.getElementById('myTable1');
  3.     var lastRow = tbl.rows.length;
  4.     for (var i=1; i < lastRow; i++) {
  5.         alert(tbl.rows[i].cells[7].innerHTML);
  6.     }
  7. }
  #7  
Old August 28th, 2008, 09:28 AM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 344

re: count the values of a table column


Quote:
Originally Posted by cleary1981
I havnt used tbody in my code. heres what I have so far

Expand|Select|Wrap|Line Numbers
  1. function total() {
  2.     var tbl = document.getElementById('myTable1');
  3.     var lastRow = tbl.rows.length;
  4.     for (var i=1; i < lastRow; i++) {
  5.         alert(tbl.rows[i].cols[7]);
  6.     }
  7. }
If I could just work out how to get the value of column7 for each row I would be sorted
[HTML]var table = document.getElementById("myTable1");
var lastRow = table.rows.length;
for (var i=1; i < lastRow; i++) {
alert(table.rows[i].cells[6].innerHTML);
}[/HTML]

This may help u out. In order to retrieve the seventh column i placed six in the cells.

Regards
Ramanan Kalirajan
  #8  
Old August 28th, 2008, 09:41 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164

re: count the values of a table column


Yeah had just worked that out. Thanks

The problem I am having now is that I am trying to add 2 values but it concatenates them instead. What can I do here?

Expand|Select|Wrap|Line Numbers
  1. function total() {
  2.     var total =0;
  3.     var tbl = document.getElementById('myTable1');
  4.     var lastRow = tbl.rows.length;
  5.     for (var i=1; i < lastRow; i++) {
  6.         var val = tbl.rows[i].cells[7].innerHTML;
  7.         var tt = total
  8.         total = tt + val;
  9.     }
  10.     document.getElementById('total').innerHTML = total;
  11. }
  #9  
Old August 28th, 2008, 09:53 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 164

re: count the values of a table column


thats me sorted. I used parseFloat and that worked a treat. thanks guys
  #10  
Old August 28th, 2008, 10:04 AM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,490
Provided Answers: 10

re: count the values of a table column


the + sign serves for mathematical addition as well as string concatenation. I think that you get concatenation because val (I think that's supposed to be a number) is retrieved by innerHTML (which is essentially a text node/node set) instead of value. You may try parseInt(), parseFloat() or Number() for conversion.
  #11  
Old August 28th, 2008, 10:05 AM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,102
Provided Answers: 1

re: count the values of a table column


Quote:
Originally Posted by cleary1981
I havnt used tbody in my code.
just a note: in IE that doesn't matter ... it always assumes a tbody ... have a look here (first comment) ... typically this problem is relevant for adding rows ... and in case you retrieve the firstChild of a table in IE it IS tbody ... even when you don't use it explicitly.

kind regards
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find X consecutive (integer) values in a table column gigonomics answers 8 January 26th, 2009 10:33 PM
Count of table column hgriva answers 6 January 11th, 2008 01:27 PM
Select Value From Specific Table Column in TableAdapter =?Utf-8?B?UmljaCBIdXRjaGlucw==?= answers 3 April 5th, 2007 03:55 PM
Repost: Values of location field gets truncated in a asp table Jack answers 12 January 19th, 2006 03:55 PM
Values of location field gets truncated in a asp generated table Jack answers 4 January 11th, 2006 08:15 PM