DOM table: number of colums

W

Ward Germonpré

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
 
M

Martin Honnen

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.
 
V

VK

Ward said:
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.
 
W

Ward Germonpré

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
 
R

RobG

VK said:
Ward said:
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
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
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.
 
V

VK

RobG said:
In more complex cases (multiple tBodies, tHead

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.
 
T

Thomas 'PointedEars' Lahn

RobG said:
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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top