How do I sum columns in TBODY?

L

loc2006

Ok, I have my data in a tbody

I want the last row of my table to sum the columns

ColA ColB ColC
<tbody id=mybod>
1 2 3
2 3 5
</tbody>
3 5 8

Is there a javascript code for this?

thanks!
 
R

Randy Webb

(e-mail address removed) said the following on 1/1/2006 12:08 PM:
Ok, I have my data in a tbody

Put it in TD's
I want the last row of my table to sum the columns
OK

ColA ColB ColC
<tbody id=mybod>
1 2 3
2 3 5
</tbody>
3 5 8

Is there a javascript code for this?
Yes.

thanks!

Welcome.
 
R

Randy Webb

(e-mail address removed) said the following on 1/1/2006 12:15 PM:

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at
the top of the article, then click on the "Reply" at the bottom of the
article headers.
Ok, i want it to sum automatically. May i ask for the Javascript code?

Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.
 
I

Ivo

(e-mail address removed) said

Post a sample of your initial effort. Putting your data in a TBODY is
not the best way to accomplish it. Place it in a TD element with ID's
that correspond to the columns.

Yes, a TBODY element only takes TR elements as its children, which take only
TD (and TH) as children. They do accept your raw text as child.
But giving every TD an ID is wasteful. Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's.
A quick and simple example, catering for different amounts of cells per row
but not for any additional markup in the TD's:

function sumup() {
var t = [], c, d = document.getElementById( 'sumtable' ),
r = d.rows, i = r.length - 1, lastrow = r;
while(i--) { c = r.cells; j = c.length;
while(j--) { if( !t[j] ){ t[j] = 0; }
t[j] += parseFloat( c[j].firstChild.nodeValue );
}
}
j = t.length; while(j--) {
if( lastrow.cells[j] ) { lastrow.cells[j].firstChild.nodeValue = t[j]; }
}
}

See it in action at
< http://4umi.com/web/javascript/tablesum.htm >
 
R

Randy Webb

Ivo said the following on 1/1/2006 3:03 PM:
Yes, a TBODY element only takes TR elements as its children, which take only
TD (and TH) as children. They do accept your raw text as child.
But giving every TD an ID is wasteful. Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's.

True. But my suggestions were not to give a full-blown answer but rather
to make the OP think and try to figure it out instead of giving a
ready-made answer.
 
P

Patient Guy

Ivo said the following on 1/1/2006 3:03 PM:

True. But my suggestions were not to give a full-blown answer but
rather to make the OP think and try to figure it out instead of giving
a ready-made answer.

It rather did look like a request for getting an answer to a school
assignment, didn't it?
 
V

VK

Patient said:
It rather did look like a request for getting an answer to a school
assignment, didn't it?

You know that, he knows that - but *they* know how to play on people's
vanity like on the piano, do they? :)
 
J

Jambalaya

Ivo wrote:
[snip]
Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's. [snip]
See it in action at
< http://4umi.com/web/javascript/tablesum.htm >

OT: Ivo, it would have more semantic value to sum all cells in the
tbody|tbodies and then to place that sum into the tfoot, rather than
placing the sum in the last tr of the tbody.
 
I

Ivo

Jambalaya said:
Ivo wrote:
[snip]
Once you have a reference to the
TABLE or TBODY element, you can walk through its childnodes and collect the
data from the TD's. [snip]
See it in action at
< http://4umi.com/web/javascript/tablesum.htm >

OT: Ivo, it would have more semantic value to sum all cells in the
tbody|tbodies and then to place that sum into the tfoot, rather than
placing the sum in the last tr of the tbody.

True, but that would complicate the function, having to retrieve a tbody as
well as a tfoot node, and I was aiming something simple and straightforward.
Moreover, the row starts its life with empty cells, how much sematic value
do you see in that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top