AJAX data in template of Select2
Solution 1:
You can pass any extra data via processResults
. In your case it can be
var options = [];
if (data) {
$.each(data.items, function (index, text) {
options.push({ id: index, text: text[0], text2: text[1] /* <-- extra data */});
});
}
return {
results: options,
more: false
};
Then access it in templateSelection
if (!state.id) {
return state.text;
}
return $(`<span>${state.text} & ${state.text2}</span>`);
http://jsfiddle.net/moshfeu/3hrupdn1/22/