471,601 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

DOM table: number of colums

Hi,

I have a reference to a dom table.
How can I retrieve the number of columns in that table ?
The stop value below doesn't work, nor did my experimenting with
tbodies[0] and childNodes..

....
var thistable = document.getElementById('resultaattbl');
for (var j=0, stop = thistable.tbody.rows[0].length; j<stop; j++) {
....
I find it bizar that the tbody shortcut works in this instance :

thistable.tbody.appendChild(newRow);

but not in the above for loop.

thx

Ward
May 15 '06 #1
6 4877


Ward Germonpré wrote:

I have a reference to a dom table.
How can I retrieve the number of columns in that table ?


Depends on how you define that (as cells can span multiple rows in HTML
tables) but you can at least read out
table.rows[0].cells.length
to find out the number of cells in the first row.

--

Martin Honnen
http://JavaScript.FAQTs.com/
May 15 '06 #2
VK

Ward Germonpré wrote:
Hi,

I have a reference to a dom table.
How can I retrieve the number of columns in that table ?
The stop value below doesn't work, nor did my experimenting with
tbodies[0] and childNodes..

...
var thistable = document.getElementById('resultaattbl');
for (var j=0, stop = thistable.tbody.rows[0].length; j<stop; j++) {
...
I find it bizar that the tbody shortcut works in this instance :

thistable.tbody.appendChild(newRow);

but not in the above for loop.


tBodies[0].rows.length // watch the case

AFAIK HTML Table doesn't have "all rows of any kind in this table"
property. Rows appertain either to tHead section or to tFoot section or
to one of tBody sections (can be as many tBodies as you want). In the
most primitive case like <table><tr><td>content</td></tr></table> and
such tHead and tFoot sections are not defined and a single tBody
section will be added automatically. In such case
tableRef.tBodies[0].rows.length-1 will be equal to the amount of rows
in the given table. In more complex cases (multiple tBodies, tHead
and/or tFoot) you have to calculate everything separately.

May 15 '06 #3
Martin Honnen <ma*******@yahoo.de> wrote in news:44686de6$0$4514$9b4e6d93
@newsread2.arcor-online.net:


Ward Germonpré wrote:

I have a reference to a dom table.
How can I retrieve the number of columns in that table ?


Depends on how you define that (as cells can span multiple rows in HTML
tables) but you can at least read out
table.rows[0].cells.length
to find out the number of cells in the first row.


Thx Martin, that was it.
Ward
May 15 '06 #4
VK wrote:
Ward Germonpré wrote:
Hi,

I have a reference to a dom table.
How can I retrieve the number of columns in that table ?
The stop value below doesn't work, nor did my experimenting with
tbodies[0] and childNodes..

...
var thistable = document.getElementById('resultaattbl');
for (var j=0, stop = thistable.tbody.rows[0].length; j<stop; j++) {
...
I find it bizar that the tbody shortcut works in this instance :

thistable.tbody.appendChild(newRow);

but not in the above for loop.
tBodies[0].rows.length // watch the case


The OP wants the number of columns, not rows. The tbody element may not
contain all the rows in table (though it can).

AFAIK HTML Table doesn't have "all rows of any kind in this table"
property.
The rows collection (an object, not a property) of a table element
contains *all* the rows in the table.

HTMLTableElement
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425>

table rows
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-6156016>

Rows appertain either to tHead section or to tFoot section or
to one of tBody sections (can be as many tBodies as you want).
tbody, thead and tfoot elements implement the table section interface,
their rows collections contain just the rows in their section of the
table. If a table has no thead or tfood and only one tbody (implied or
explicit), the the tbody's rows collection will be the same as the table's.

HTMLTableSectionElement
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-67417573>

table section rows
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-52092650>

In the
most primitive case like <table><tr><td>content</td></tr></table> and
such tHead and tFoot sections are not defined and a single tBody
section will be added automatically. In such case
tableRef.tBodies[0].rows.length-1 will be equal to the amount of rows
in the given table. In more complex cases (multiple tBodies, tHead
and/or tFoot) you have to calculate everything separately.


No, you don't; you can use the table's rows collection.
--
Rob
May 15 '06 #5
VK

RobG wrote:
In more complex cases (multiple tBodies, tHead
and/or tFoot) you have to calculate everything separately.


No, you don't; you can use the table's rows collection.


Hah! So I was overly accurate with the DOM all this time. Nice to know.

May 15 '06 #6
RobG wrote:
VK wrote:
AFAIK HTML Table doesn't have "all rows of any kind in this table"
property.


The rows collection (an object, not a property) of a table element
contains *all* the rows in the table.


JFTR, `rows' "is" both: a property of HTMLTableElement/
HTMLTableSectionElement objects (because it is an attribute
of the HTMLTableElement/HTMLTableSectionElement interface),
and (a reference to) a HTMLCollection object (with items
consisting of references to HTMLTableRowElement objects).
PointedEars
--
http://members.ud.com/download/gold/
http://folding.stanford.edu/
http://alien.de/seti/
http://setiathome.ssl.berkeley.edu/
May 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Rutter | last post: by
41 posts views Thread by Ruby Tuesday | last post: by
117 posts views Thread by phil-news-nospam | last post: by
13 posts views Thread by Nico VanHaaster | last post: by
2 posts views Thread by Harshpandya | last post: by
5 posts views Thread by jrod11 | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by CCCYYYY | last post: by

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.