How to fix server returning wrong json object back on create event?

How to fix server returning wrong json object back on create event?

jsomwebjsomweb Posts: 8Questions: 4Answers: 0

Hi,
Because of the Select2 plugin has a bug on optgroup as I mentioned in https://datatables.net/forums/discussion/34030
I change the dom for select fields on editor open event.

editor
.on( 'open', function ( e, type ) {
if(!labsDrop.length) {
loadLabs(function(result){
labsDrop = result;
});
}
if(!studentsDrop.length) {
loadStudents(function(result){
studentsDrop = result;
});
}
if(!facultyDrop.length) {
loadFaculty(function(result){
facultyDrop = result;
});
}
$('#DTE_Field_assign_labs-lid').parent().empty().append(labsDrop.prop('outerHTML'));
$('#DTE_Field_assign_labs-sid').parent().empty().append(studentsDrop.prop('outerHTML'));
$('#DTE_Field_assign_labs-hour').parent().empty().append(loadHours(0));
$('#DTE_Field_assign_labs-fid').parent().empty().append(facultyDrop.prop('outerHTML'));
$('.updated-lab').select2();
$('.updated-ta').select2();
$('.ta-hour').select2(
{
minimumResultsForSearch: Infinity
}
);
$('.updated-faculty').select2();
} )
.on( 'preSubmit', function ( e, data, action ) {
if(action == 'create') {
var $box = $('.DTE_Action_Create');
var row = data.data[Object.keys(data.data)[0]].assign_labs;
row.fid = $box.find('.updated-faculty').val();
row.hour = $box.find('.ta-hour').val();
row.lid = $box.find('.updated-lab').val();
row.sid = $box.find('.updated-ta').val();
row.strm = global.actSem;
}
})
.on('postSubmit',function( e, json, data, action ){
console.log(json);
console.log(data);
});

The object can be created and submitted to the database successfully, however the new row updated in the front end table is always wrong.

I printed out the data that I submitted and the json object returned by the server in console.

.on('postSubmit',function( e, json, data, action ){
    console.log(json);//json object return by server
    console.log(data);//data sent to server
});

Submitted data will be printed out correctly, but the json object returned by server is shown as a random row that nothing related to the new object I just submitted.

It is really wired and I can not explain why. Can anybody help? Thank you!

Answers

This discussion has been closed.