Problems setting field value in Editor
Problems setting field value in Editor
Please can someone help me with this as I cannot see what I am doing wrong.
I am trying to set the value of a field when there has been a change to another. In the cutdown example below, if the value of the test.name field is changed during an edit, I want to programmatically update the test.nameStatus field with "Name Changed" when the record is saved. (line 47)
The output from console.log works fine, its just that I cannot seem to set the test.nameStatus field. I have tried preEdit and preSubmit. I realise there is no logic to this example, but I have reduced it to this to illustrate my issue.
(function($){
$(document).ready(function() {
var editor = new $.fn.dataTable.Editor( {
ajax: 'php/table.test.php',
table: '#test',
submit: 'allIfChanged',
fields: [
{
"label": "name:",
"name": "test.name"
},
{
"label": "Name Status:",
"name": "test.nameStatus"
}
]
} );
var table = $('#test').DataTable( {
dom: 'Bfrtip',
ajax: 'php/table.test.php',
columns: [
{
"data": "test.name"
},
{
"data": "test.nameStatus"
}
],
select: true,
lengthChange: false,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor }
]
} );
var previousName;
editor.on('initEdit', function () {
previousName = this.val('test.name')
});
editor.on('preEdit', function () {
if (previousName !== this.val('test.name')){
editor.field('test.nameStatus').set('Name Changed');
console.log('Name Changed');
}
});
} );
}(jQuery));
Thank you in advance.
Replies
That is the most frequent use case for "dependent".
https://editor.datatables.net/reference/api/dependent()
Here are some examples from my own coding. You can also have dependencies on arrays of fields. Don't forget the callback! Can be a little tricky if you have asynchronous ajax calls in your code.
Thank you very much indeed @rf1234 . This has been a great help and will update once I get it working
Hi @rf1234, Thank you very much, all working perfectly.