Prast
Prast

Reputation: 57

JQuery add delete row table from another

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?

Upvotes: 1

Views: 2107

Answers (1)

Peter &#214;rneholm
Peter &#214;rneholm

Reputation: 2858

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/

Upvotes: 7

Related Questions