Select2 fields resetting to first option on nested datatable/editor update/delete
Select2 fields resetting to first option on nested datatable/editor update/delete
I'm having an issue with my Select2 dropdown fields where they reset to the first value in the list (however it may be sorted, usually alphabetically in my cases) whenever a nested datatable row is updated or deleted. The problem does NOT occur when adding a new entry to the nested tables, nor does it occur when the field type is a standard select field. Am I doing something wrong? Or is there some Select2 event I can interrupt on edit/delete?
I do have this set up in a live environment we're using for a sort of beta test, so I can set you up with a login to try it out if you'd like.
//nested editor
var issueEditor = new $.fn.dataTable.Editor( {
ajax: './php/issues_nested.php',
template: '#issueForm',
fields: [ {
label: 'Issue',
name: 'mtrf_requests.issue',
className: 'block'
}, {
label: 'Reported By',
name: 'mtrf_requests.reported_by',
className: 'block'
}, {
label: 'Work Performed',
name: 'mtrf_requests.work_performed',
type: 'textarea',
className: 'block'
}, {
label: 'Complete',
name: 'mtrf_requests.complete',
type: 'checkbox',
className: 'block',
options: [
{ label: '', value: 1 }
],
separator: '',
unselectedValue: 0
}
]
} );
//main editor
editor = new $.fn.dataTable.Editor( {
ajax: './php/inbox_table.php',
table: "#inbox",
template: "#customForm",
fields: [ {
//this field acts normally
label: "Client ID",
name: "mtrf_work_orders.client_id",
// type: "select2",
// opts: {
// placeholder: "Client...",
// allowClear: true
// },
type: 'select',
className: 'block'
}, {
//this field gets reset any time 'dummy_issues' is edited or deleted. Untouched when a new entry is added.
label: "Unit Type",
name: "mtrf_work_orders.unit_type",
type: "select2",
id: 'unitType',
opts: {
placeholder: "Unit Type...",
allowClear: true
},
className: 'block'
}, {
label: 'Issues',
name: 'dummy_issues',
type: 'datatable',
editor: issueEditor,
config: {
ajax: {
url: './php/issues_nested.php',
type: 'POST',
//send post variable of form ID to DB query script
data: function (d) {
d.form_id = form_id;
}
},
buttons: [
{ extend: 'create', editor: issueEditor },
{ extend: 'edit', editor: issueEditor },
<?php if($_SESSION['user']['access_level']>3) { echo "{ extend: 'remove', editor: issueEditor }, "; } ?>
{
extend: 'refresh',
editor: issueEditor,
attr: {
id: 'issuesRefresh'
}
}
],
columns: [
{
title: 'Issue',
data: 'mtrf_requests.issue'
},
{
title: 'Work Performed',
data: 'mtrf_requests.work_performed'
},
{
title: 'Status',
data: 'mtrf_requests.complete',
render: function(data, type, row) {
return (data == 1) ? "Complete" : "";
}
}
]
}
}