DataTables Editor Select2 Empty Default Value w/ AJAX request.
DataTables Editor Select2 Empty Default Value w/ AJAX request.
Hi all,
I've been banging my head off the wall for several hours trying to figure out how to do something I would figure would be pretty easy to accomplish. I am using the Select2 plug in with an Editor field, to query the database and bring back suggested values based on the input, and that part works just fine. However, when I open an edit form, the default value for the Select2 field is blank. I would like for it to use the value that is already present in the DataTable.
I've attempted to use the InitEdit edit event to try to and manually create an option but didn't have any luck.
Any suggestions would be greatly appreciated.
Here's the Editor code
{
name: "Item",
label: "Item",
"type": "select2",
"initialValue": true,
"opts": {
ajax: {
url: '/Home/getItems',
dataType: 'json',
initialValue: true,
data: function (params) {
var query = {
search: params.term,
type: 'public'
}
return query;
},
processResults: function (data) {
return {
results: JSON.parse(data)
};
}
},
minimumInputLength: 1
}
}
Answers
When the Select2 plug-in is configured with the
ajax
property, it will make an Ajax call to the server as soon as the value is set when editing is triggered. It will send andinitData
parameter to let the server know that it should respond with just a single value and label for the value submitted.Allan