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? - <table border="2" cellspacing="2" cellpadding="2" width=100% id="myTable1">
-
<tr>
-
<th> </th>
-
<th>Part no.</th>
-
<th>Description</th>
-
<th>Price (£)</th>
-
<th>Quantity</th>
-
<th>Discount</th>
-
<th>Supplier</th>
-
<th>Sub Total</th>
-
</tr>
-
</table>
-
<span>£<span id ="total"></span></span>
heres the javascript I use to add a row - var tbl = document.getElementById('myTable1');
-
var lastRow = tbl.rows.length;
-
// alert (lastRow);
-
// if there's no header row in the table, then iteration = lastRow + 1
-
var iteration = lastRow;
-
var row = tbl.insertRow(lastRow);
-
-
// cell number
-
var cellLeft = row.insertCell(0);
-
var textNode = document.createTextNode(iteration);
-
cellLeft.appendChild(textNode);
-
-
// part number
-
var cellpno = row.insertCell(1);
-
var t_pno = document.createTextNode(part_no);
-
cellpno.appendChild(t_pno);
-
-
//description
-
var celldesc = row.insertCell(2);
-
var t_desc = document.createTextNode(description);
-
celldesc.appendChild(t_desc);
-
-
//price
-
var cellprice = row.insertCell(3);
-
var t_price = document.createTextNode(price);
-
cellprice.appendChild(t_price);
-
-
//quantity
-
var cellqty = row.insertCell(4);
-
var t_qty = document.createTextNode(quantity);
-
cellqty.appendChild(t_qty);
-
-
//discount
-
var celldisc = row.insertCell(5);
-
var t_disc = document.createTextNode(discount);
-
celldisc.appendChild(t_disc);
-
-
//supplier
-
var cellsup = row.insertCell(6);
-
var t_sup = document.createTextNode(supplier);
-
cellsup.appendChild(t_sup);
-
-
//subtotal
-
var cellsub = row.insertCell(7);
-
var sub = quantity * price;
-
var sub1 = (sub / 100) * discount;
-
var sub2 = sub - sub1;
-
var result = sub2.toFixed(2);
-
var t_sub = document.createTextNode(result);
-
cellsub.appendChild(t_sub);
-
-
document.getElementById('part_no').value = "";
-
document.getElementById('quantity').value = "";
-
document.getElementById('description').value = "";
-
document.getElementById('price').value = "";
-
document.getElementById('discount').value = "";
-
total();
10 3337
I'm not sure if you want to count rows or columns, nevertheless try following: - document.getElementById("mytable1").childNodes.length
-
// should count tr elements (except when you use tbody)
-
document.getElementById("mytable1").firstChild.childNodes.length
-
// should count td elements of the first row
regards
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?
gits 5,390
Expert Mod 4TB
hi ...
try the following: - 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
I havnt used tbody in my code. heres what I have so far - function total() {
-
var tbl = document.getElementById('myTable1');
-
var lastRow = tbl.rows.length;
-
for (var i=1; i < lastRow; i++) {
-
alert(tbl.rows[i].cols[7]);
-
}
-
}
If I could just work out how to get the value of column7 for each row I would be sorted
ive worked it out - function total() {
-
var tbl = document.getElementById('myTable1');
-
var lastRow = tbl.rows.length;
-
for (var i=1; i < lastRow; i++) {
-
alert(tbl.rows[i].cells[7].innerHTML);
-
}
-
}
I havnt used tbody in my code. heres what I have so far - function total() {
-
var tbl = document.getElementById('myTable1');
-
var lastRow = tbl.rows.length;
-
for (var i=1; i < lastRow; i++) {
-
alert(tbl.rows[i].cols[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
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? - function total() {
-
var total =0;
-
var tbl = document.getElementById('myTable1');
-
var lastRow = tbl.rows.length;
-
for (var i=1; i < lastRow; i++) {
-
var val = tbl.rows[i].cells[7].innerHTML;
-
var tt = total
-
total = tt + val;
-
}
-
document.getElementById('total').innerHTML = total;
-
}
thats me sorted. I used parseFloat and that worked a treat. thanks guys
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
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 ...
|
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 ...
|
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,...
|
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...
|
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...
|
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",...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |