Editor table with JOIN and SELECT Boxes
Editor table with JOIN and SELECT Boxes
cemlimited
Posts: 36Questions: 9Answers: 0
Hi Guys,
I have an Inline whole row create table in editor.
Everything working fine.....Apart from the Create.....Will not update with the new entry.
I know this is associated with the selects in the field list....
Cannot figure this one out.
Thanks Steve
var editor; // use a global for the submit and return data rendering in the examples
$(document).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "/DTEditor/controllers/stoppages.php",
table: "#example",
fields: [ {
label: "PWO:",
name: "downtime_records.pwo",
type: "readonly",
def: "P53063*58" ,
},
{
label: "Manager:",
name: "downtime_records.manager",
type: "readonly",
def: 1 ,
},
{
label: "minutes:",
name: "downtime_records.minutes"},
{
label: "reason:",
name: "downtime_records.reason",
type: "select"
},
{
label: "costed:",
name: "downtime_records.costed",
type: "select"
},
]
} );
// Activate an inline edit on click of a table cell
$('#example').on( 'click', 'tbody td.row-edit', function (e) {
editor.inline( table.cells(this.parentNode, '*').nodes(), {
submitTrigger: this,
submitHtml: ''
} );
} );
// Delete row
$('#example').on( 'click', 'tbody td.row-remove', function (e) {
editor.remove( this.parentNode, {
title: 'Delete record',
message: 'Are you sure you wish to delete this record?',
buttons: 'Delete'
} );
} );
var table = $('#example').DataTable( {
dom: "Bfrtip",
ajax: "/DTEditor/controllers/stoppages.php",
order: [[ 1, 'asc' ]],
columns: [
{ data: "downtime_records.pwo"},
{ data: "downtime_records.manager"},
{ data: "downtime_records.minutes"},
{ data: "downtime_reasons.reason", editField: "downtime_records.reason" },
{ data: "costed.costed", editField: "downtime_records.costed" },
{
data: null,
defaultContent: '',
className: 'row-edit dt-center',
orderable: false
},
{
data: null,
defaultContent: '',
className: 'row-remove dt-center',
orderable: false
},
],
select: {
style: 'os',
selector: 'td:first-child'
},
buttons: [ {
extend: "createInline",
editor: editor,
formOptions: {
submitTrigger: -2,
submitHtml: ''
}
} ]
} );
} );
$table = "downtime_records";
$fieldquery = "SHOW Columns FROM `".$table."`";
$pwo = $_GET['pwo'];
$result2 = $db->prepare($fieldquery);
$result2->execute();
foreach ($result2 as $row) {
$fields[] =$row['Field'];
}
$stoppage_excludes = array("id");
$stoppage_display = array("pwo","manager");
$stoppage_selects = array("reason","costed");
$stoppage_selects_tables = array("downtime_reasons","costed");
$stoppage_selects_labels = array("reason","costed");
// DataTables PHP library
include( $_SERVER["DOCUMENT_ROOT"]."/DTEditor/lib/DataTables.php" );
$db->sql("SET names 'utf8'");
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
// Build our Editor instance and process the data coming from _POST
$aaa =Editor::inst( $db, $table );
if($pwo == ''){
}else{
$aaa->where('pwo', $pwo);
}
$aaa ->field(Field::inst( 'downtime_records.id'));
foreach(array_diff($fields, $stoppage_selects,$stoppage_excludes) as $field){
$aaa ->field(Field::inst( $table.".".$field));
}
$cycle = 0;
foreach($stoppage_selects as $select){
$aaa ->field(Field::inst($table.'.'.$select)->options( Options::inst()->table( $stoppage_selects_tables[$cycle] )->value( 'id' )->label( $stoppage_selects_labels[$cycle] ))->validator( Validate::dbValues()));
$aaa ->field(Field::inst( $stoppage_selects_tables[$cycle].'.'.$stoppage_selects_labels[$cycle]));
$aaa ->leftJoin( $stoppage_selects_tables[$cycle],$stoppage_selects_tables[$cycle].'.id', '=', $table.'.'.$select );
$cycle = $cycle + 1;
}
$aaa->debug(true)->process( $_POST )->json();
This question has an accepted answers - jump to answer
Answers
Additional Information:
Here is the Controller output.
We resolved this by e-mail so, for anyone else who finds this thread, the key was to disable the writing of the primary key field by using:
In general there is no need to include the primary key, unless you want to show it to the end user. There are some rare cases where you might want the end user to write a value for the primary key, and if you do include the id in the fields list, Editor expects a value to be submitted for writing, unless
->set(false)
is specified.Allan
As always - Allan has sorted. Gentleman.
Thanks again.