Good morning,
I followed the directions in this article https://datatables.net/blog/2019-01-11
but I can't pass the "DOMANDA_ID" variable to risposte-elenco-invia.php
i think the problem is here, d.domanda = rowData.DOMANDA_ID; and here def: rowData.DOMANDA_ID but i don't know what to put in
function createChild ( row ) {
var rowData = row.data();
var table = $('<table class="display" width="100%"/>');
row.child( table ).show();
var risposteEditor = new $.fn.dataTable.Editor( {
ajax: {
url: 'risposte-elenco-invia.php',
data: function ( d ) {
d.domanda = rowData.DOMANDA_ID;
}
},
table: table,
fields: [
{
label: "DOMANDA ID:",
name: "risposte.DOMANDA_ID",
def: rowData.DOMANDA_ID
}, {
label: "ESATTA:",
name: "risposte.RISPOSTA_ESATTA"
}, {
label: "TESTO:",
name: "risposte.RISPOSTA_TESTO"
}
]
});
var risposteTable = table.DataTable( {
dom: 'Bfrtip',
ajax: {
url: 'risposte-elenco-invia.php',
type: 'post',
data: function ( d ) {
d.domanda = rowData.DOMANDA_ID;
}
},
columns: [
{ title: 'Id', data: 'risposte.RISPOSTA_ID' },
{ title: 'Domanda', data: 'risposte.DOMANDA_ID' },
{ title: 'Esatta', data: 'risposte.RISPOSTA_ESATTA' },
{ title: 'Testo', data: 'risposte.RISPOSTA_TESTO' }
],
select: true,
buttons: [
{ extend: 'create', editor: risposteEditor },
{ extend: 'edit', editor: risposteEditor },
{ extend: 'remove', editor: risposteEditor }
]
} );
risposteEditor.on( 'submitSuccess', function (e, json, data, action) {
row.ajax.reload(function () {
$(row.cell( row.id(true), 0 ).node()).click();
});
} );
}
function updateChild(row) {
$("table", row.child())
.DataTable()
.ajax.reload();
}
function destroyChild(row) {
// Remove and destroy the DataTable in the child row
var table = $("table", row.child());
table.detach();
table.DataTable().destroy();
// And then hide the row
row.child.hide();
}
$(document).ready(function() {
var domandeEditor = new $.fn.dataTable.Editor( {
ajax: "domande-elenco-invia.php",
table: "#domande",
fields: [
{
label: "Testo:",
name: "domande.TESTO"
},{
label: "Legge:",
name: "domande.LEGGE_ID",
type: "select"
},{
label: "Materia:",
name: "domande.MATERIA_ID"
}, {
label: "Concorso",
name: "domande.CONCORSO_ID"
}, {
label: "Test n.",
name: "domande.TEST_ID"
}
]
} );
var domandeTable = $('#domande').DataTable( {
dom: "Bfrtip",
ajax: "domande-elenco-invia.php",
aLengthMenu: [[50, 100, 200, -1], [50, 100, 200, "Tutti"]],
columns: [
{
data: null,
defaultContent: '',
className: 'details-control',
orderable: false
},
{ data: "domande.DOMANDE_ID" },
{ data: "domande.TESTO" },
{ data: null, render: function(data, type, row, meta) {
return row.legge.LEGGE_N + '/' + row.legge.LEGGE_ANNO
}},
{ data: "domande.MATERIA_ID" },
{ data: "domande.CONCORSO_ID" },
{ data: "domande.TEST_ID"}
],
order: [ 1, 'desc' ],
select: {
style: 'os',
selector: 'td:not(first-child)'
},
buttons: [
{ extend: "create", editor: domandeEditor },
{ extend: "edit", editor: domandeEditor },
{ extend: "remove", editor: domandeEditor }
]
} );
$('#domande tbody').on('click', 'td.details-control', function () {
var tr = $(this).closest('tr');
var row = domandeTable.row(tr);
if ( row.child.isShown() ) {
// This row is already open - close it
destroyChild(row);
tr.removeClass('shown');
}
else {
// Open this row
createChild(row);
tr.addClass('shown');
}
});
domandeEditor.on("submitSuccess", function() {
domandeTable.rows().every(function() {
if (this.child.isShown()) {
updateChild(this);
}
});
});
});
file risposte-elenco-invia.php
if ( !isset($_POST['domanda']) || !is_numeric($_POST['domanda']) ) {
echo json_encode( [ "data" => [] ] );
}
else {
$db->sql("SET names 'utf8'");
Editor::inst( $db, 'risposte', 'RISPOSTA_ID')
->fields(
Field::inst( 'risposte.RISPOSTA_ID' ),
Field::inst( 'risposte.DOMANDA_ID' ),
Field::inst( 'risposte.RISPOSTA_ESATTA' ),
Field::inst( 'risposte.RISPOSTA_TESTO' )
)
->leftJoin( 'domande', 'domande.DOMANDE_ID', '=', 'risposte.DOMANDA_ID' )
->where ("risposte.DOMANDA_ID", $_POST['domanda'])
->process( $_POST )
->json();
}