Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > DOM table: number of colums

Reply
Thread Tools

DOM table: number of colums

 
 
Ward Germonpré
Guest
Posts: n/a
 
      05-15-2006
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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      05-15-2006


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/
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-15-2006

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.

 
Reply With Quote
 
Ward Germonpré
Guest
Posts: n/a
 
      05-15-2006
Martin Honnen <(E-Mail Removed)> 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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      05-15-2006
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
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      05-15-2006

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.

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      05-22-2006
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I display data in bounded colums? =?Utf-8?B?YmFzdWxhc3o=?= ASP .Net 1 05-11-2005 03:46 PM
Adding HeaderText and HeaderImageUrl to Colums in DataGrid Patrick Olurotimi Ige ASP .Net 1 03-11-2005 01:37 PM
Dynamic width/number of colums Ryan Stewart HTML 0 12-16-2004 05:20 PM
DataGrid Button Colums Javascript !! Amit Garg ASP .Net 0 01-22-2004 11:17 AM
Dynamic Colums blake lawless ASP .Net 0 11-21-2003 10:22 PM



Advertisments