Zabs Zabs - 1 year ago 87
Javascript Question

Unable to select result from select2 dropdown

I have the following javascript/jQuery that pulls in data from via AJAX. That works fine however I can cannot seem to be able to select an option from the dropdown? Can anyone explain what I have done wrong..

This is something to do with the id..

ajax: {
url: "/admin/generator/teams",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term, // search term
processResults: function (data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used = || 1;

return {
results: data.items,
pagination: {
more: ( * 30) < data.total_count
cache: true
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
minimumInputLength: 1,
id: function(data){ return data._id; },

Answer Source

First you should make sure processResults function returns an array of objects with id and text properties like this:

{ id: '1', text: 'Option 1' }

Then once the options are loaded, you can set the value by calling:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download