W3C home > Mailing lists > Public > www-dom@w3.org > October to December 1999

Rearranging rows in a table

From: Jeff Fink <jfink@ryos.com>
Date: Wed, 13 Oct 1999 13:02:26 -0400 (EDT)
To: <www-dom@w3.org>
Message-ID: <002c01bf159c$928bf330$868d2dc7@meesh.ryos.com>

I have this table:
<TABLE ID="tbl">
	<THEAD>
	<TR>
		<TH>Col 0</TH>
	</TR>
	</THEAD>

	<TBODY>
	<TR>
		<TD>Row 0</TD>
	</TR>
	<TR>
		<TD>Row 1</TD>
	</TR>
	</TBODY>
</TABLE>

I want to switch the order of the rows in the table body and there does not
seem to be a straightforward way to do this. I can write this code:

	var pTable = document.getElementById("tbl");
	var pRow0 = pTable.tBodies[0].rows[0];
	var pRow1 = pTable.tBodies[0].rows[1];

	var pNewRow = pTable.tBodies[0].insertRow(0);
	var pCell, i, j;

	for(i = 0; i < pRow1.cells.length; i++)
	{
		pCell = pNewRow.insertCell(i);
		for(j = 0; j < pRow1.cells[i].childNodes.length; j++)
			pCell.appendChild(pRow1.cells[i].childNodes[j]);
	}

	pTable.tBodies[0].deleteRow(2);


However, if I've set any attributes on the HTMLTableRowElement or on the
HTMLTableCellElement via setAttribute(), they get lost because I can't clone
these elements and insert them into the new table.  Why aren't there
insertRow() and insertCell() methods that allow you to pass the node you
want for the new Row/Cell?  If these methods were in place, this problem
would become:

	var pTable = document.getElementById("tbl");
	var pRow1 = pTable.tBodies[0].rows[1];

	pTable.tBodies[0].insertRow(pRow1, 0);

And of course, all my attributes get moved over because I'm moving a single
node rather than copying everything.
-Jeff
Received on Thursday, 14 October 1999 10:16:27 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:46 GMT