better way of swapping columns and rows in a table (code provided)

Discussion in 'Javascript' started by foldface, Sep 7, 2004.

  1. foldface

    foldface Guest

    Hi
    This works but is a bit long-winded. Is there a better way?
    ta
    F


    <style>
    ..Header
    {
    background-color: gray;
    }

    </style>

    <table id=theTable>
    <tr>
    <td class=Header nowrap style="bob: 1">Header1</td>
    <td class=Header>Header2</td>
    <td class=Header>Header3</td>
    <td class=Header>Header4</td>
    <td class=Header>Header5</td>
    </tr>
    <tr>
    <td> Contents01 </td>
    <td> Contents02 </td>
    <td> Contents03 </td>
    <td> Contents04 </td>
    <td> Contents05 </td>
    </tr>
    <tr>
    <td> Contents11 </td>
    <td> Contents12 </td>
    <td> Contents13 </td>
    <td> Contents14 </td>
    <td> Contents15 </td>
    </tr>
    </table>

    <hr>
    <input type=button value=PressMe onclick="func()">

    <script>

    function func()
    {
    var theTable = document.getElementById('theTable')
    var theTable2 = document.createElement("table");

    var numRows = theTable.rows.length;
    var numCells = theTable.rows[0].cells.length;

    for(var i=0; i<numCells; i++)
    {
    var newRow = theTable2.insertRow(-1)
    for(var j=0; j<numRows; j++)
    {
    var cell = theTable.rows[j].cells;
    var newCell = newRow.insertCell(-1);
    newCell.innerHTML = cell.innerHTML;
    newCell.className = cell.className;
    newCell.style.cssText = cell.style.cssText
    }
    }

    while (theTable.rows.length > 0)
    {
    theTable.deleteRow(0);
    }

    var numRows = theTable2.rows.length;
    var numCells = theTable2.rows[0].cells.length;
    debugger;
    for(var i=0; i<numRows; i++)
    {
    var newRow = theTable.insertRow(-1)
    for(var j=0; j<numCells; j++)
    {

    var cell = theTable2.rows.cells[j];
    var newCell = newRow.insertCell(-1);
    newCell.innerHTML = cell.innerHTML;
    newCell.className = cell.className;
    newCell.style.cssText = cell.style.cssText
    }
    }

    while (theTable2.rows.length > 0)
    {
    theTable2.deleteRow(0);
    }
    }
    </script>
     
    foldface, Sep 7, 2004
    #1
    1. Advertisements

  2. foldface

    foldface Guest

    ..and where the heck did style class 'bob: 1'
    I think most people would recognise that as the equivilent of
    hello world and not bother posting about it
    which naturally on comp.lang.javascript is the only bit I care about
     
    foldface, Sep 8, 2004
    #2
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.