472,986 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 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 4936


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ajay Garg | last post by:
What is the way we could implement a business logic from a Table by storing it statemnnets in a colums and defining an execute sql to execute it.Some legal requirements make it diffcult for us to...
3
by: Phil Rutter | last post by:
Hello All, I have about 700 word documents that have 2 tables one is static 4 colums x 5 rows the other is 5 colums x rows ranging from 2 to 100 what i wolud like to do is open the word doc....
1
by: Eric | last post by:
Hello, I have a Existing table with three columns that have a data type of string. I want to write VBA code that will modify one of the colums to DataType of Number. Also, I'm looking for code...
41
by: Ruby Tuesday | last post by:
Hi, I was wondering if expert can give me some lite to convert my word table into access database. Note: within each cell of my word table(s), some has multi-line data in it. In addition, there...
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
13
by: Nico VanHaaster | last post by:
Hello all. There is what i would like to do, I have a page that pulls all the hours for our staff over a given period calculated them and reports back on 15 different colums and is aproximatly...
0
by: ravindarjobs | last post by:
Hello friends, I am using ms access 2003, and Vb6. i am displaying a table in a form as pivot table (following "Auto Form: Pivot Table" Wizard) Usually i select the items i want to be...
2
by: Harshpandya | last post by:
Here is my code for look up table - where i am planning to built 10 colums and 8 rows and put array values in the table boxes. Problem is - it is only printing the last array valules instead of all...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.