Convert TD columns into TR rows

后端 未结 4 790
时光取名叫无心
时光取名叫无心 2020-11-28 12:42

Is there a quick way to translate (using CSS or Javascript) a tables TD into TR, currently I have:

A B C D
1 2 3 4

and I want to translate

4条回答
  •  醉话见心
    2020-11-28 12:46

    Here is a tested function that will transpose a table, and it will preserve any formatting/events you had hooked to any elements within the table (ie: onclicks on cell or cell contents)

    function TransposeTable(tableId)
    {        
        var tbl = $('#' + tableId);
        var tbody = tbl.find('tbody');
        var oldWidth = tbody.find('tr:first td').length;
        var oldHeight = tbody.find('tr').length;
        var newWidth = oldHeight;
        var newHeight = oldWidth;
    
        var jqOldCells = tbody.find('td');        
    
        var newTbody = $("");
        for(var y=0; y");
            for(var x=0; x

    Notes: - Requires Jquery - Not tested on very large tables - Will likely crap out on tables with spanned columns/rows - Will likely crap out on tables with any combination of thead/th

    So as long as your tables have no spanned cells and doesnt use thead/th, should be good to go.

提交回复
热议问题