Hello!
I’m using the approach found in: ‘/data-sources/ajax’ for multiple selections using ajax:
$(".js-example-data-ajax").select2({
ajax: {
url: “https://api.github.com/search/repositories”,
dataType: ‘json’,
delay: 250,
data: function (params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function (data, params) {
…
}
placeholder: ‘Search for a repository’,
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
It is working great when used interactively, templateSelection function is receiving as first parameter the JSON item from the ajax response. However, when I try to add selected options programmatically as shown in ‘/programmatic-control/add-select-clear-items’:
// Set up the Select2 control
$(’#mySelect2’).select2({
ajax: {
url: ‘/api/students’
}
});
// Fetch the preselected item, and add to the control
var studentSelect = $(’#mySelect2’);
$.ajax({
type: ‘GET’,
url: ‘/api/students/s/’ + studentId
}).then(function (data) {
// create the option and append to Select2
var option = new Option(data.full_name, data.id, true, true);
studentSelect.append(option).trigger(‘change’);
//Other listeners will in fact receive params.data correctly; however, templateSelection will not
studentSelect.trigger({
type: ‘select2:select’,
params: {
data: data
}
});
});
Then, templateSelection receives as ‘data’ parameter an standard JSON:{ disabled: false
element: option
id: “11898”
selected: true
text: “option text”
title: “”
_resultId: “select2–result-8vg3-11898”}
Instead of the actual data returned in the ajax call.
How can I add an option programmatically in such a way the same behavior as when used interactively is replicated and templateSelection gets the ajax returned ‘data’ as parameter?