473,406 Members | 2,769 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,406 software developers and data experts.

How do I sum columns in TBODY?

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!

Jan 1 '06 #1
9 4660
lo*****@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 1 '06 #2
Ok, i want it to sum automatically. May i ask for the Javascript code?

Jan 1 '06 #3
lo*****@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 1 '06 #4
Ivo
"Randy Webb" wrote
lo*****@gmail.com said
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.


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[i];
while(i--) { c = r[i].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 >

--
hth
ivo
http://4umi.com/web/javascript/
Jan 1 '06 #5
Ivo said the following on 1/1/2006 3:03 PM:
"Randy Webb" wrote
lo*****@gmail.com said
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.

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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 2 '06 #6
Randy Webb <Hi************@aol.com> wrote in
news:B8********************@comcast.com:
Ivo said the following on 1/1/2006 3:03 PM:
"Randy Webb" wrote
lo*****@gmail.com said

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.

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.


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

Jan 2 '06 #7
VK

Patient Guy wrote:
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? :-)

Jan 2 '06 #8
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.

Jan 3 '06 #9
Ivo
"Jambalaya" <ch*********@gmail.com> wrote
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?

--
hth
ivo
http://4umi.com/web/javascript/
Jan 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Freddy Morel | last post by:
My objective is to make a two columns table from an XML-data file. I do not want to use XSL, so sorting and else is not important for the moment. I have this working in a table:...
3
by: David Lee | last post by:
I have some web pages on which I'd like to suppress certain items when printing. This is mostly working nicely: index.htm (here stripped to relevant essentials) has: <head> <link...
17
by: Matt Kruse | last post by:
I'm looking for the best JS/CSS solution to add functionality to tables. The only browser which needs to be supported is IE5.5+, but no activeX can be used. to be able to do: - Fixed header row...
16
by: matt | last post by:
hello, ive been trying to figure something out, largely thru trial & error. thought perhaps someone else may have knowledge. i have an html table that consists of blocks of related data --...
5
by: Romulo NF | last post by:
Greetings, I´m back here to show the new version of the drag & drop table columns (original script ). I´ve found some issues with the old script, specially when trying to use 2 tables with...
3
by: AshishMishra16 | last post by:
Hi Friends, What is the way to iterate the Table and Make all the input types editable on the click of a checkbox. Here is what My Requirement is : <table id="adSearchTable"...
5
by: r_ahimsa_m | last post by:
Hello, I am lerning HTML/CSS/JavaScript. I created HTML page with table "property_fields" containing 24 rows ('tr' elements). I want to remove last 23 rows: var table =...
2
by: bgold12 | last post by:
I have a problem that can be simplified to the following: I have a table with three columns. I need the middle column to be fixed width, let's say 100px. The other two columns I want to be variable...
2
by: angelicdevil | last post by:
i m trying to display the result images in table format or rows and columns but the cod i wrote displays them only in rows....someone plz help <?php // generating a array with image paths...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.