Copy rows between 2 tables

Copy rows between 2 tables

jpavanjpavan Posts: 38Questions: 12Answers: 0

I need to have 2 tables on the same page. one of them will contain the sources rows. I want to select rows from the source and copy them to the other to record a database.

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    At the most basic level you would use something like:

    var sourceRow = sourceTable.row( ... );
    
    destTable.row.add( sourceRow.data() ).draw();
    
    sourceRow.remove().draw();
    

    which takes the data from a row in the source table, copies it to the destination table and then removes the original row in the source table.

    If you want to do drop and drag between the tables, that is of course possible as well, although I haven't written any open source code to do that yet, so you'd need to start fresh on that.

    Allan

  • jpavanjpavan Posts: 38Questions: 12Answers: 0
    edited June 2018

    Perfect Allan.

    If table1 is the source and table2 the destination, this is my code for move a row on click it:

         $ ('# table1 tbody'). on ('click', 'tr', function () {
               var source = table1.row(this);
               table2.row.add(source.data()).draw ();
               source.remove().draw ();
             });
    

    Thank you very much,

This discussion has been closed.