Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22667

Re: Duplicating the last row of a Table

Message-ID <31598702.t5zLgSxMMz@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-01-18 12:53 +0100
Subject Re: Duplicating the last row of a Table
Newsgroups comp.lang.javascript
References <ZemAs3VzQa2SFwhs@invalid.uk.co.demon.merlyn.invalid> <bju1j4Fjf5sU1@mid.individual.net>

Show all headers | View raw


Zlatko Duric wrote:

> That's a good behavior,

*What* is good behavior?  Please do not top-post.

<http://PointedEars.de/faq#posting>

> because with the first quoted statement you're not getting a tbody
> element. You're getting its rows, and an array has no method appendChild
> (by default).

The object does not have an appendChild() method by default, indeed.
But that is _not_ due its being an Array; it is due to its _implementing
the HTMLCollection interface_.

<http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/html.html#ID-64060425>
<http://www.w3.org/TR/2013/CR-html5-20130806/tabular-data.html#the-table-element>

> On 01/17/2014 10:44 PM, Dr J R Stockton wrote:
> 
>  > var Rose = document.getElementById("TB").rows
> 
> [cut]
> 
>  > Rose.appendChild(newnode);
> 
> [cut]
> 
>  > Cel = Rose.item(Y).cells.item(X)

  Cel = Rose[Y].cells[X];

suffices.  But this is not Pascal; variable identifiers that do not stand 
for constructors or constant-like values should start lowercase.

> So, you can do something like this to get the tbody:
> 
>      var tb = document.getElementById('tb');

  var tb = document.getElementById('TB');

Another possibility is to use table.tBodies[index].

> and then use that in further code and append a row to tb.

It was asked for *duplicating* the last row of a table, and that is easily 
done:

  var rows = table.rows;
  var last_row = rows[rows.length - 1];
  last_row.parentNode.appendChild(last_row.cloneNode(true));

or

  last_row.parentNode.insertBefore(last_row.cloneNode(true), null);

I am using “last_row.parentNode” instead of “table” because the parent node 
may be a “tbody” element node.  [Cloning is vital because otherwise the 
original row is moved to the end of the table or table section (you can use 
that behavior to sort table rows quickly).]
 
> You get rows with something like document.getElementById('table-id').rows.

That accesses the NodeList of rows of the table, not of a table body, 
though.  Note that at least Chromium WebKit sorts the rows of the table in 
*display* order; i.e., the rows for the table footer (“tfoot” element) come 
last even though the corresponding “tr” elements do not.  When in doubt, 
access the rows of the table sections instead.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Duplicating the last row of a Table Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-01-17 21:44 +0000
  Re: Duplicating the last row of a Table Zlatko Duric <zladuric@gmail.com> - 2014-01-18 01:52 +0100
    Re: Duplicating the last row of a Table Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-18 12:53 +0100
      Re: Duplicating the last row of a Table RobG <rgqld@iinet.net.au> - 2014-01-19 17:00 +1000
        Re: Duplicating the last row of a Table Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-19 14:41 +0100
  Re: Duplicating the last row of a Table "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-01-18 12:16 +0100
    Re: Duplicating the last row of a Table Dr J R Stockton <reply1400@merlyn.demon.co.uk.invalid> - 2014-01-19 17:00 +0000
  Re: Duplicating the last row of a Table Danny <dann90038@gmail.com> - 2014-01-18 11:32 -0800

csiph-web