tags:

views:

1202

answers:

6

If I have a table as shown below, and have a up and down arrow that moves rows up and down, how would I go about swapping rows in JQuery?

<tr id="Row1">
  <td>Some label</td>
  <td>Some complex control</td>
</tr>
<tr id="Row2">
  <td>Some label</td>
  <td>Some complex control</td>
</tr>
<tr id="Row3">
  <td>Some label</td>
  <td>Some complex control</td>
</tr>
+2  A: 

Here's a plugin that does drag and drop table rows

Ólafur Waage
+4  A: 
$("#Row1").after($("#Row2"));

will work

cobbal
+1  A: 

To move Row1 one step down, you'd do:

$me = $("#Row1");
$me.after($me.nextSibling());
svinto
A: 

I would try:

var tmp = $ ('#Row1')
$ ('#Row1').remove
$ ('#Row2').after ($ ('#Row1'))

But I guess it’s better to swap rows’ contents instead of swapping rows themselves, so that you can rely on numbering. Thus,

var tmp = $ ('#Row1').html ()
$ ('#Row1').html ($ ('#Row2').html ())
$ ('#Row2').html (tmp)
Ilya Birman
remove can mess with event handlers though, so I try to avoid it
cobbal
Oh, thanks for info, will be careful with it :-) My second example does not use it, though.
Ilya Birman
+2  A: 

Here's another solution.

To move a row down:

jQuery("#rowid").next().after(jQuery("#rowid"));

To move a row up:

jQuery("#rowid").prev().before(jQuery("#rowid"));
A: 

how does one add effects for the swapping of rows? :P

dude