Parent / Child select multiple rows in parent table

Parent / Child select multiple rows in parent table

rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

I am frequently using parent and child tables as described in this blog: https://datatables.net/blog/2016-03-25

I pass the id of the selected parent table record to the child table as per the blog example. What I need to do now is to pass multiple selected ids of the parent table to the child table and subsequently edit all of the child records that fit.

This is the child table in Java Script:

var infomaContractTable = $('#tblInfomaContract').DataTable( {
    ajax: {
        url: 'actions.php?action=tblInfomaContract',
        type: 'POST',
        data: function ( d ) {
            var selected = contractTable.row( {selected: true} ); 
            if (selected.any()) {
                d.contract_id = selected.data().contract.id;
            }
        }
    },        
    columns: [           
        { data: "contract_has_infoma.external_id" }
    ]
} );

In PHP I do this:

if ( ! isset($_POST['contract_id']) || ! is_numeric($_POST['contract_id']) ) {
        echo json_encode( [ "data" => [] ] );
    } else {        
        Editor::inst( $db, 'contract_has_infoma' )
        ->field(
            Field::inst( 'contract_has_infoma.contract_id' )->set(Field::SET_CREATE)
                                                            ->setValue( $_POST['contract_id'] ) ....
         ->leftJoin( 'contract', 'contract_has_infoma.contract_id', '=', 'contract.id')
        ->where( 'contract_has_infoma.contract_id', $_POST['contract_id'] )

How could I do the same for multiple parent records being selected and passed to the child table? Will ->setValue work as well and how? Any ideas? Many thanks for trying to help!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin
    Answer ✓

    You'd need to use something like this on the client-side:

                var selected = contractTable.rows( {selected: true} );
                if (selected.any()) {
                    d.contract_ids = selected.data().pluck('contract').pluck('id').join(',');
                }
    

    Note how it uses rows() rather than row() to get multiple rows. Then I set d.contract_ids to be a comma separated string of the ids.

    You'd need to modify the server-side PHP to use or_where() to get all of the rows for the matching ids (rather than a single = condition).

    Also the create new row method might cause you some issues since you'd need to select which of the ids you want to attach the row to. Perhaps one option for that would be to allow the user to select from a select which one they want.

    Allan

  • rf1234rf1234 Posts: 2,986Questions: 87Answers: 421

    Thanks! Will try that later probably using where() with 'IN' and a list of the ids.

This discussion has been closed.