Re-order table columns?

后端 未结 4 876
北荒
北荒 2021-02-03 10:55

Does anyone know a way to re-order table columns, using jQuery?

I don\'t mean sort - I mean dynamically move entire columns left or right in a table.

I\'m aware

4条回答
  •  故里飘歌
    2021-02-03 11:20

    The idea is to walk over all rows (tr's) of the table and swap the desired td's. Let's swap column 2 and column 4:

    $('table tr').each(function() {
        var tr = $(this);
        var td1 = tr.find('td:eq(1)'); // indices are zero-based here
        var td2 = tr.find('td:eq(3)');
        td1.detach().insertAfter(td2);
    });
    

    I hope this helps.

提交回复
热议问题