I have three tables:
TABLE entrepots :
id_entrepots
int(200) NOT NULL,
nom_entrepots
varchar(150) NOT NULL,
TABLE equipements :
nom_equipements
varchar(200) NOT NULL,
groupe_equipements
varchar(200) NOT NULL,
periodicite_equipements
varchar(100) NOT NULL
TABLE controles :
id_controles
int(200) NOT NULL,
date_controles_realiser
date NOT NULL,
date_prochain_controles
date NOT NULL,
date_rdv
date NOT NULL,
etat_controles
varchar(10) NOT NULL,
With the right leftovers to make it all work.
My problem:
When I open the modal to edit the data, I want to update several columns after modifying the date_controles_realiser
.
example
date_controles_realiser
in database is 2023-06-12
If I change it to 2024-06-12 and click on UPDATE :
Datatable will then display the new date in date_prochain_controles
according to the data in the periodicite_equipements
column (Annual, monthly, half-yearly, ...).
EX: if it's Annual, the new date date_prochain_controles
will be 2024-06-12 and etat_controles
will be "GREEN" if date_controles_realiser
is less than or equal to the current date AND the date_rdv
will be empty.
But I have no idea how to implement this Datatable Editor (version 1.9)
my code :
var editor; // use a global for the submit and return data rendering in the examples
// Activate an inline edit on click of a table cell
$('#example').on( 'click', 'tbody td.editable', function (e) {
editor.inline( this, {
//onBlur: 'submit'
buttons: { label: ">",
className: 'btn-sm',
fn: function () { this.submit(); } }
} );
} );
$('#example').DataTable( {
initComplete: function () {
this.api().columns([1,2,3,4,5,13]).every( function () {
var column = this;
var select = $('<select><option value="">ALL</option></select>')
.appendTo( $(column.footer()).empty() )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? ''+val+'' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
},
dom: "Bfrtip",
ajax: {
url: "Staff_4.php",
type: 'POST'
},
serverSide: false,
columns: [
{
data: null,
defaultContent: '',
className: 'select-checkbox',
orderable: false,
searchable: false
},
{ data: "controles.etat_controles", className: "dt-body-center" ,
target: [1],
type: "alt-string",
render: function ( data, type, row ) {
if (type !== 'display') {
return data;
}
const icons = {
"BLANC": '<img src="img/blanc.gif" width="34" height="18" alt="BLANC" title="BLANC"/>',
"NOIR": '<img src="img/noir.gif" width="34" height="18" alt="NOIR" title="NOIR"/>',
"VERT": '<img src="img/vert.gif" width="34" height="18" alt="VERT" title="VERT"/>',
"ORANGE": '<img src="img/orange.gif" width="34" height="18" alt="ORANGE" title="ORANGE"/>',
"ROUGE": '<img src="img/rouge.gif" width="34" height="18" alt="ROUGE" title="ROUGE"/>',
"BLEU": '<img src="img/bleu.gif" width="34" height="18" alt="BLEU" title="BLEU"/>',
};
const values = data.split(', ');
let result = '';
values.forEach((value) => {
if (value.trim() in icons) {
result += icons[value.trim()];
} else {
result += value.trim();
}
result += ' '; // add space between values
});
return result.trim(); // remove extra space at the end
}
},
{ data: "entrepots.nom_entrepots", editField: "controles.fk_entrepots", className: "dt-body-left", searchable: true },
{ data: "equipements.nom_equipements", editField: "controles.fk_equipements", className: "dt-body-left", searchable: true },
{ data: "equipements.groupe_equipements", className: "editable dt-body-center" },
{ data: "equipements.periodicite_equipements", className: "editable dt-body-center" },
{ data: "controles.date_controles_realiser", className: "editable dt-body-center" },
{ data: "controles.date_prochain_controles", className: "editable dt-body-center" },
{ data: "controles.date_rdv", className: "editable dt-body-center" },
{ data: "controles.commentaire", className: "dt-body-center"},
{ data: "controles.lien_fichier1", className: "dt-center",
render: function ( data, type, row, meta ) {
if (data.startsWith('https://')){
return '<a href="'+data+'" target="_blank"><i class="fa fa-cloud" style="font-size:30px;color:lightgreen;text-shadow:2px 2px 4px #000000;" /></a>';
}else{return data;}
}
},
{ data: "controles.lien_fichier2", className: "dt-body-center",
render: function ( data, type, row, meta ) {
if (data.startsWith('https://')){
return '<a href="'+data+'" target="_blank"><i class="fa fa-cloud" style="font-size:30px;color:lightgreen;text-shadow:2px 2px 4px #000000;" /></a>';
}else{return data;}
}
},
{ data: "controles.lien_fichier3", className: "dt-body-center",
render: function ( data, type, row, meta ) {
if (data.startsWith('https://')){
return '<a href="'+data+'" target="_blank"><i class="fa fa-cloud" style="font-size:30px;color:lightgreen;text-shadow:2px 2px 4px #000000;" /></a>';
}else{return data;}
}
},
{ data: "controles.actif_controles", className: "dt-body-center" }
],
order: [ 2, 'asc' ],
select: {
style: 'os',
selector: 'td:first-child'
},
lengthMenu: [
[ 10, 25, 50, -1 ],
[ '10 rows', '25 rows', '50 rows', 'Show all' ]
],
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor },
{
extend: 'collection',
text: 'Export',
buttons: [
'copy',
'excel',
'csv',
'pdf',
'print'
]
},
{ extend: 'pageLength', editor: editor }
]
} );
} );
And server :
Editor::inst( $db, 'controles', 'id_controles' )
->field(
//Field::inst( 'controles.id_controles' ),
Field::inst( 'controles.etat_controles' ),
Field::inst( 'controles.fk_entrepots' )
->options( Options::inst()
->table( 'entrepots' )
->value( 'id_entrepots' )
->label( 'nom_entrepots' )
)
->validator( Validate::dbValues() ),
Field::inst( 'entrepots.nom_entrepots' ),
Field::inst( 'controles.fk_equipements' )
->options( Options::inst()
->table( 'equipements' )
->value( 'id_equipements' )
->label( 'nom_equipements' )
)
->validator( Validate::dbValues() ),
Field::inst( 'equipements.nom_equipements' ),
Field::inst( 'equipements.groupe_equipements' ),
Field::inst( 'equipements.periodicite_equipements' ),
Field::inst( 'controles.date_controles_realiser' )
->validator( Validate::dateFormat( 'Y-m-d' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
->setFormatter( Format::dateFormatToSql('Y-m-d' ) )
->getFormatter( function ( $val, $data, $opts ) {
if ($val === "0000-00-00"){ echo ""; }else{ return date( 'Y-m-d', strtotime( $val ) ); }
} ),
Field::inst( 'controles.date_prochain_controles' )
->validator( Validate::dateFormat( 'Y-m-d' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
->setFormatter( Format::dateFormatToSql('Y-m-d' ) )
->getFormatter( function ( $val, $data, $opts ) {
if ($val === "0000-00-00"){ echo ""; }else{ return date( 'Y-m-d', strtotime( $val ) ); }
} ),
Field::inst( 'controles.date_rdv' )
->validator( Validate::dateFormat( 'Y-m-d' ) )
->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
->setFormatter( Format::dateFormatToSql('Y-m-d' ) )
->getFormatter( function ( $val, $data, $opts ) {
if ($val === "0000-00-00"){ echo ""; }else{ return date( 'Y-m-d', strtotime( $val ) ); }
} ),
Field::inst( 'controles.commentaire' ),
Field::inst( 'controles.lien_fichier1' ),
Field::inst( 'controles.lien_fichier2' ),
Field::inst( 'controles.lien_fichier3' ),
Field::inst( 'controles.actif_controles' )
)
->leftJoin( 'entrepots', 'entrepots.id_entrepots', '=', 'controles.fk_entrepots' )
->leftJoin( 'equipements', 'equipements.id_equipements', '=', 'controles.fk_equipements' )
->debug(false)
->process($_POST)
->json();
![]()
thank you for your help as I don't know how to go about it