I want to invert table tbody rows with jQuery.
WHAT I HAVE:
<table width="630" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>TITLE A</td>
<td>TITLE B</td>
(...) continue in jsfiddle.
Here what I have and what I want: http://jsfiddle.net/ZaUrP/1/
This can be done in two quick steps: With any cell in your table selected, go to the Ablebits Data tab > Transform group, and click Flip > Vertical Flip.
Select an empty cell below your pasted table. Click the arrow under the "Paste" button on the ribbon and choose the "Transpose" option. Your table will appear flipped.
Try this:-
Get the array of tr
s from tbody
using .get()
and use Array.reverse to reverse the elements and assign it back.
var tbody = $('table tbody');
tbody.html($('tr',tbody).get().reverse());
In case you have events to tr
or any containing elements you could just attach it using delegation, so that the reversed elements also get them delegated.
fiddle
pretty much the same as the other guy, only I use .detach() which is guarunteed to keep any crazy events that were attached to the tr
s intact. I also use $.makeArray to avoid reversing any of the proto
stuff on the base jQuery object.
$(function(){
$("tbody").each(function(elem,index){
var arr = $.makeArray($("tr",this).detach());
arr.reverse();
$(this).append(arr);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With