Search code examples
javascriptjquerymootoolspivot-table

JavaScript code or framework for converting columns to rows and rows to columns in HTML table or div grids


I am looking for a script or JavaScript framework plugin which can help me to manipulate HTML grids. On some event call, columns and rows should be interchanged.


Solution

  • That's a simple function, assuming that there are exactly n*m cells (no colspans/rowspans, no varying row widths):

    function translate(tableel) {
        var rows = tableel.rows,
            m = rows.length,
            n = rows[0].cells.length;
        for (var i=0; i<n; i++) {
            var r = tableel.insertRow(-1);
            for (var j=0; j<m; j++)
                r.appendChild(rows[j].cells[0]);
        }
        for (var i=0; i<m; i++)
            tableel.deleteRow(0);
    }
    

    Would be much more complicated with jQuery :-)