Skip to content Skip to sidebar Skip to footer

Use Jquery To Move Columns In Order To Next When Page Is Resized

Currently I am laying out information like so in the website: td1 td2 td3 td4 td5 td6 td7 td8 When resizing the browser that the 4th and 8th td are now cut off, I would like this t

Solution 1:

I never heard of anything like that, and it`s kinda silly to do that.

I wrote something quickly. You can check it here

http://jsfiddle.net/M2JBS/37/

It does need more work, but if you really need to use tables it`s a start I guess...

You would have to take in consideration classes applied to tables, recalculate widths and put them back to the page again. Also you would need to make them fit the table itself.

It would be easier to pull all TDs from the table and create divs from that.

HTML

<divid="tableContainer"><tableclass="destroy"border="1"><tr><td>test 1</td><td>test 2</td><td>test 3</td></tr><tr><td>test 4</td><td>test 5</td><td>test 6</td></tr><tr><td>test 7</td><td>test 8</td><td>test 9</td></tr><tr><td>test 10</td><td>test 11</td><td>test 12</td></tr><tr><td>test 13</td><td>test 14</td><td>test 15</td></tr></table></div>

​ ​ Javascript:

window.onresize = function() {
    customizeTables()
};

functioncustomizeTables() {
    // new tablevar nt = newArray();
    // current table tdsvar elements = newObject();
    // table parent widthvar wrap = $('table').parent().width();
    // current generated widthvar ct = 0;
    var fw = 0;
    // col of new tablevar col = newArray();

    $('table.destroy td').each(function(index, ob) {

        fw = $(ob).width() + 2; //borders
        fw += parseFloat($(ob).css('padding-left').replace("px", ""));
        fw += parseFloat($(ob).css('padding-right').replace("px", ""));
        fw += parseFloat($(ob).css('margin-left').replace("px", ""));
        fw += parseFloat($(ob).css('margin-right').replace("px", ""));

        if ((ct + fw) <= wrap) {

            ct += fw;
        } else {

            nt.push(col);
            ct = fw;
            col = [];
        }

        col.push(ob);
        // all elements (all tds)
        elements[index] = ob;
    });
    nt.push(col);

    var $table = $('<table class="destroy" border="1">');
    var row = '';

    $.each(nt, function(row, cols) {

        var row = '<tr>';

        for (i in cols) {
            row += '<td>' + $(cols[i]).html() + '</td>';
        }

        $table.append(row + '</tr>');

    });

    $table.append('</table>');
    $('#tableContainer').empty();
    $table.appendTo('#tableContainer');
}
customizeTables();​

Post a Comment for "Use Jquery To Move Columns In Order To Next When Page Is Resized"