Copy selected rows and certain columns from one table to another.

Copy selected rows and certain columns from one table to another.

bbrindzabbrindza Posts: 300Questions: 69Answers: 1

Does anyone have a good working example of being able to copy selected data rows and only specific columns from one DataTable to another? I want to invoke a function to do this from a button click.

I saw some really good examples of copying an entire table to another, however I could not find what I was looking for.

Thanks in advance

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,466Questions: 26Answers: 4,804

    How are you selecting the rows?

    This example shows how to get the row data when the row is clicked. This example shows how to get the selected rows when using the Select extension.

    Once you have the row data you can manipulate it if need before copying to the second table. Use row.add() or rows.add() as appropriate to add the rows to the second table.

    Kevin

  • bbrindzabbrindza Posts: 300Questions: 69Answers: 1

    Using the Select Extension which gives me the input checkbox.

        copyMachineTasksTable = $('#copyMachineTasksTable').DataTable( {
    
              pageLength: 10,
              searching: false,
              paging: false,
              info: false,
              ordering: false,
    
              ajax: {
                       type: 'POST',
                       url: "MachineTaskManagement/ssp_TaskManagement.php",
                       data: {locationCode: userLocationCode,
                              machineCode: $("#copyTasksMachineSelectionDialogModal_machineCode").val()},   
                       cache: 'false',
                },
            columnDefs: [
                     {
                        targets: 0,
                        checkboxes: {
                        selectRow: true
                        }
                     }
                  ],
             columns: [ { 
                             data: null,
                             defaultContent: '',
                         },        
                                  
                        { className: 'detail-level-control',
                          orderable: false,
                          data: null,
                          defaultContent: '',
                          width: '5px'},
                        { data: "location_code", "visible": false }, 
                        { data: "machine_code", "visible": false }, 
                        { data: "task_number" },
                        { data: "scheduled_pm_number" },
                        { data: "last_pm_date" }, 
                        { data: "pm_scheduled" },
                        { data: "type_of_pm" },
                        { data: "next_pm_date" },
                        { data: "interval_to_next_pm" },
                        { data: "meter_reading" },
                        { data: "meter_scheduled_amount" }
                    ],
             select: {
                        style: 'multi'
                     },
    
          } );
    
  • bbrindzabbrindza Posts: 300Questions: 69Answers: 1

    Kevin,

    What about also being able to only bring over specific columns to the new table?

  • kthorngrenkthorngren Posts: 20,466Questions: 26Answers: 4,804
    Answer ✓

    You have these columns in the source table:

                    { data: "location_code", "visible": false },
                    { data: "machine_code", "visible": false },
                    { data: "task_number" },
                    { data: "scheduled_pm_number" },
                    { data: "last_pm_date" },
                    { data: "pm_scheduled" },
                    { data: "type_of_pm" },
                    { data: "next_pm_date" },
                    { data: "interval_to_next_pm" },
                    { data: "meter_reading" },
                    { data: "meter_scheduled_amount" }
    

    Lets say you want only these columns:

                    { data: "task_number" },
                    { data: "scheduled_pm_number" },
    

    You can simply use row.add() and Datatables will populate that row with all the source column data but only display the columns you define. Or you can use something like the delete operator to remove the objects you don't want to copy. You will need to manipulate the source data to match what you want to be copied.

    Kevin

  • bbrindzabbrindza Posts: 300Questions: 69Answers: 1

    Cool. I will get that a whirl. Thank you .

Sign In or Register to comment.