DOM table: number of colums

Discussion in 'Javascript' started by Ward Germonpré, May 15, 2006.

  1. 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
     
    Ward Germonpré, May 15, 2006
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, May 15, 2006
    #2
    1. Advertising

  3. Ward Germonpré

    VK Guest

    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.
     
    VK, May 15, 2006
    #3
  4. Martin Honnen <> 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
     
    Ward Germonpré, May 15, 2006
    #4
  5. Ward Germonpré

    RobG Guest

    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
     
    RobG, May 15, 2006
    #5
  6. Ward Germonpré

    VK Guest

    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.
     
    VK, May 15, 2006
    #6
  7. 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/
     
    Thomas 'PointedEars' Lahn, May 22, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amit Garg

    DataGrid Button Colums Javascript !!

    Amit Garg, Jan 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    932
    Amit Garg
    Jan 22, 2004
  2. blake lawless

    Dynamic Colums

    blake lawless, Nov 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    blake lawless
    Nov 21, 2003
  3. Ryan Stewart

    Dynamic width/number of colums

    Ryan Stewart, Dec 16, 2004, in forum: HTML
    Replies:
    0
    Views:
    456
    Ryan Stewart
    Dec 16, 2004
  4. Ioannis

    Table colums asymmetrical

    Ioannis, Oct 3, 2005, in forum: HTML
    Replies:
    0
    Views:
    1,063
    Ioannis
    Oct 3, 2005
  5. Replies:
    1
    Views:
    376
Loading...

Share This Page