473,372 Members | 868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

count the values of a table column

178 100+
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();
Aug 27 '08 #1
10 3331
Dormilich
8,658 Expert Mod 8TB
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
Aug 27 '08 #2
cleary1981
178 100+
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?
Aug 28 '08 #3
gits
5,390 Expert Mod 4TB
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
Aug 28 '08 #4
cleary1981
178 100+
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
Aug 28 '08 #5
cleary1981
178 100+
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. }
Aug 28 '08 #6
RamananKalirajan
608 512MB
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
Aug 28 '08 #7
cleary1981
178 100+
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. }
Aug 28 '08 #8
cleary1981
178 100+
thats me sorted. I used parseFloat and that worked a treat. thanks guys
Aug 28 '08 #9
Dormilich
8,658 Expert Mod 8TB
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.
Aug 28 '08 #10
gits
5,390 Expert Mod 4TB
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
Aug 28 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Aaron C | last post by:
Hi, I'm trying to do an insert with the following statement: INSERT INTO user VALUES ( 'ag@ag.com','ag','Aaron','Chandler','','','La Mirada','CA',90638,714,'',''); and I'm getting the error...
1
by: t123 | last post by:
I have a select count() that seams be ignoring one clause. Data: Table: ID CA-Char CB-Int 1 dsfsd 6 2 dsfsd 0 3 dsfsd 1 4 sdrtt 1 SQL is:
22
by: Joseph Shraibman | last post by:
On a 7.3.4 database: explain analyse select count(*) from elog; Aggregate (cost=223764.05..223764.05 rows=1 width=0) (actual time=81372.11..81372.11 rows=1 loops=1) -> Seq Scan on elog ...
4
by: vacataire testeur de site web | last post by:
Bonjour je cherche à afficher de count de tables identiques dans la forme exemple num_cli | nombre de poste | nombre de voiture 1 1 3 2 2 ...
0
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
1
by: sammy | last post by:
If you have a select with 2 attributes where you group by one attribute and do a count() for the second attribute, if the count() is 0 then that row is never displayed. How would you instead see...
2
by: DrewKull | last post by:
Ok ... so I'm looking at a query where im trying to show a bunch of columns counted up based on criteria ... So far i've been able to count all the rows up and show them ... and also count up...
1
by: jmarr02s | last post by:
I used the following query supplied by Jorn, SELECT IIf(=15 Or =19, "15 og 19",IIf(=13 Or =17,"13 og 17",IIf(=16 Or =20,"16 og 20",IIf(=14 Or =18,"14 og 18",IIf(=9, "9",...
2
by: Pete | last post by:
I need to create a single query (Not a SQL query) against a single table that counts the number of records in the table, where the single field "tmp" contains specific string values If the field...
1
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.