Click to See Complete Forum and Search --> : Collapsing a table


mjxnjx
August 19th, 2003, 03:08 PM
Hello,

I'm working on some code to collapse a table.
By collapsing I mean returning only the rows with a unique value on a particular column.

For example:
A
B
C
A
B
D

would return a new table with only
A
B
C
D

I'm close, but i'm having trouble with appending the new rows to the table. Maybe someone can help me out here...



function testCollapseTable(table,col)
{
var oldrows = createRowsArray(table);
var newrows = new Array();
newrows.length = 0;
if (oldrows.length > 0)
{
for (var index1=0;index1<oldrows.length;index1++)
{
var bFound = false;
for (var index2=0;index2<newrows.length;index2++)
{
var cell1 = oldrows[index1].cells[col].firstChild.nodeValue;
var cell2 = newrows[index2].cells[col].firstChild.nodeValue;
if (cell1 == cell2)
bFound = true;
}
if (!bFound)
newrows[newrows.length] = oldrows[index1];
}
}
for (var clearIndex = 0; clearIndex < oldrows.length; clearIndex++)
table.deleteRow(oldrows[clearIndex].rowIndex);
for (var r = 0; r < newrows.length; r++)
table.rows[r] = newrows[r];
}

function createRowsArray (table)
{
var rows = new Array();
var r = 0;
for (var r = 0; r < table.rows.length; r++)
rows[r] = table.rows[r];
return rows;
}