I am trying to use the Select2 plugin to have 4 dropdown lists that depend on each other. I have struggled to find the right way to update the data that loads the options in.
My goal is to load the new data via ajax, but once I have it in the client I am unable to add the new data to the select list.
The code I have tried is here:
$(#"a3").select2({
placeholder: "select an item",
allowClear: true}).on("change",
function (e) {
var results = $.get("url?id=2",
function (data, textStatus, jqXHR) {
$(this).select2({ data: { results: data, text: "Name" } });
});
}
);
There is another question here select2 changing items dynamically but the solution there worked with Select2 v3.2 but not Select2 v3.3
select2({...})
over and over again (because Knockout) was causing me massive memory leaks in some browsers. Just a caveat to what you wrote: Select2 will throw an error if you try to just return the data. You need to return an object where the data is in theresults
index. So:return {results: data}
– Unmask