Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  6] [ 5]  / answers: 1 / hits: 19329  / 13 Years ago, wed, july 20, 2011, 12:00:00

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 of the excellent dragtable plugin, but I don't need something that allows the user to move columns, I need something that will do the re-ordering in a configurable way.


More From » jquery

 Answers
15

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.


[#91104] Monday, July 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
heidys

Total Points: 665
Total Questions: 102
Total Answers: 97

Location: Belarus
Member since Sat, Jul 18, 2020
4 Years ago
;