views:

152

answers:

1

I have two tables with FIRST and SECOND id.

<TABLE ID="FIRST">
<TR>
<TD></TD>
<TD></TD>
<TD></TD
</TR>
</TABLE>

<TABLE ID="SECOND"> 
<TR>
<TD>1</TD>
<TD>First Value</TD>
<TD><A HREF="#">Add</A></TD>
</TR>
<TR>
<TD>2</TD>
<TD>Second Value</TD>
<TD><A HREF="#">Add</A></TD>
</TR>
<TR>
<TD>...</TD>
<TD>...</TD>
<TD><A HREF="#">Add</A></TD>
</TR>
</TABLE>

My goal is when i click Add link, the row will move from table2 to table1 with Add link become Delete link, reorder table1 and table2. When I click Delete link on table1, the row will move from table1 to table 2,, reorder table1 and table2.

How can i implement it using JQuery

+3  A: 

This will do that for you:

$(function() {
   function moveRow(row, targetTable, newLinkText){
       $(row)
           .appendTo(targetTable)
           .find("A")
               .text(newLinkText);
   }

   $("#FIRST A").live("click", function(){
       moveRow($(this).parents("tr"), $("#SECOND"), "Add");
   });

   $("#SECOND A").live("click", function(){
       moveRow($(this).parents("tr"), $("#FIRST"), "Delete");
   });
});​

http://jsfiddle.net/UxRVa/1/

To sort the table, use something like: http://tablesorter.com/docs/

Peter Forss