Reputation: 2189
I'm just trying to load the data for a select2 dropdown using Ajax, instead of putting the large dataset inline in my HTML.
$('input[name="field"]').select2({
ajax: {
url: "/data.json",
dataType: "json",
results: function(data,page)
{
return data;
}
}
});
This works, in that it waits until I "open" my select2 list before making the Ajax call to get the data. It then also displays the data correctly. However, it isn't filtering the list as I type. Instead, it makes repeated ajax calls to (presumably) get the filtered data.
Also, if I set an existing value in my form, it doesn't appear in my select2 control. I'm guessing this is because I'm not using initSelection
but I'm not clear on how to do this correctly.
Am I doing this all wrong? It appears that what I really want is the functionality of data
but with remote loading.
Upvotes: 1
Views: 3986
Reputation: 2189
I don't think this is ideal, but it works:
$.get("/data.json", function(data)
{
$('input[name="field"]').select2({
data: data,
});
});
Upvotes: 4