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?

1
  • +1 @womp - We need to see what you've done first. Commented Jul 20, 2010 at 16:03

1 Answer 1

7

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.