4

What's the elegant way to do this?

user198729
  • 61,774
  • 108
  • 250
  • 348
  • Maybe this link would help you a bit: http://stackoverflow.com/questions/444801/move-x-number-of-rows-in-a-table-using-jquery – Paul Peelen Dec 04 '09 at 08:26

6 Answers6

7

Here's a quick plugin I wrote for you. Call it on a table, and give it the the old row and the new row position.

$.fn.extend({ 
  moveRow: function(oldPosition, newPosition) { 
    return this.each(function(){ 
      var row = $(this).find('tr').eq(oldPosition).remove(); 
      $(this).find('tr').eq(newPosition).before(row); 
    }); 
   } 
 }); 

$('#myTable').moveRow(4, 3);

Here's an example on jsbin: http://jsbin.com/uroyi

Alex Sexton
  • 10,401
  • 2
  • 29
  • 41
  • +1 - very succinct answer. If the `