Even though I am able to populate the dropdown from the data source, there is an issue with filtering the results using the search field at the top. If I make an AJAX request to the API, fetch the data, create <option>
elements for each result, and append them to the parent <select>
, then initialize Select2 - everything works smoothly.
$("#athletes").select2({
ajax: {
url: myUrl,
type: "GET",
dataType: "json",
processResults: function(data) {
return { results: data.results };
}
}
});
However, when utilizing the built-in ajax function of Select2, the results are retrieved but the search functionality is not working properly. No matter what keyword is entered in the search field, all results are displayed. It appears that adding additional query parameters to the request and implementing server-side filtering is the standard approach. Is there a way to populate the dropdown from the source and still utilize the search feature as if it was a pre-populated <select>
element?