Here is my code for setting up a select2
function setupCoinSelect() {
$('#coin').select2({
placeholder: 'Select a coin',
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection,
ajax: {
type: "get",
url: "/home/getcoinlist",
dataType: 'json',
processResults: function (data) {
console.log(data);
var arr = $.map(data, function (element) {
return {
id: element.coinId,
text: element.name,
icon: element.icon,
lastPriceUsd: element.lastPriceUsd
};
});
console.log(arr);
return { results: arr };
},
}
});
$('#coin').on('select2:select', function (e) {
selectedCoin = e.params.data;
if (selectedCoin) {
$("#add-alert-current-coin-price").text(selectedCoin.lastPriceUsd);
addAlertValuesChanged();
$("#add-alert-options").fadeIn();
}
});
}
and here is the code i use to reset it
$('#add-alert-modal').on('hidden.bs.modal', function () {
$("#coin").select2('data', null);
$("#select2-coin-container").text("Select a coin");
});
What happens is that the control seems to have been reset but let say I have selected “Item 1” before closing the model, if i open the model again and search (ajax) and pick “Item 1” again it suppose to work but it doesnt seem to do anything, however if i choose a different Item every time after a close then it works, it seems that the control somehow retains some data for the item selected and when the same item is selected twice (before and after a close) it does nothing
My question is, what is the correct way to reset a control complete.
I have actually tried destroying the control and recreating it on close but that doesnt seem to work either