Parent / Child select multiple rows in parent table
Parent / Child select multiple rows in parent table
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
You'd need to use something like this on the client-side:
Note how it uses
rows()
rather thanrow()
to get multiple rows. Then I setd.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
Thanks! Will try that later probably using where() with 'IN' and a list of the ids.