Last active
August 29, 2015 14:14
-
-
Save romeshniriella/0384111cfb7f88e7a3e5 to your computer and use it in GitHub Desktop.
Select2 Grouped Static Data With AJAX Fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"results":[ | |
{ | |
"text":"Country", | |
"children":[ | |
{ | |
"id":11082, | |
"text":"SRI LANKA (LK)" | |
}, | |
{ | |
"id":10627, | |
"text":"MAURITIUS (MU)" | |
} | |
] | |
}, | |
{ | |
"text":"Airport", | |
"children":[ | |
{ | |
"id":17269, | |
"text":"Katunayake Airport" | |
} | |
] | |
}, | |
{ | |
"text":"City", | |
"children":[ | |
{ | |
"id":25845, | |
"text":"Kandy" | |
}, | |
{ | |
"id":25829, | |
"text":"Galle" | |
} | |
] | |
} | |
] | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var initLocationLookup = function (element, apiUrl, dataItems, opt) { | |
opt = opt || {}; | |
// retrieve these from initial ajax query or somewhere | |
var dt = { results: [{ | |
"text":"Country", | |
"children":[ | |
{ | |
"id":11082, | |
"text":"SRI LANKA (LK)" | |
}, | |
{ | |
"id":10627, | |
"text":"MAURITIUS (MU)" | |
} | |
] | |
}]}; | |
$(element).select2({ | |
minimumInputLength: 0, // important!!! => without setting to 0 the default data will not be shown | |
ajax: { | |
// Select2's ajax helper | |
url: apiUrl, | |
dataType: 'json', | |
data: function (term, page) { | |
return { | |
q: term, // search term | |
page_limit: 10 | |
}; | |
}, | |
results: function (data, page) { | |
// data from server is something like | |
// {"Locations":[{"text":"Country","children":[{"id":11107,"text":"UNITED KINGDOM (GB)"}]}]} | |
return { results: data.Locations }; | |
} | |
}, | |
initSelection: function (element, callback) { | |
// if there's an initial value set in the input control, use it to query the data | |
var id = element.val(); | |
if (id) { | |
$.ajax({ | |
url: apiUrl + "?q=" + id | |
}).done(function (data) { | |
// data from server is something like | |
// {"Locations":[{"text":"Country","children":[{"id":11107,"text":"UNITED KINGDOM (GB)"}]}]} | |
if (data.Locations && data.Locations.length > 0 && data.Locations[0].children && data.Locations[0].children.length > 0) { | |
callback(data.Locations[0].children[0]); | |
} | |
}); | |
} | |
}, | |
multiple: false, | |
query: function (query) { | |
// here we check whether the user has entered some search term | |
// and enforce a min term length to 2 chars | |
if (query.term && query.term.length > 2) | |
$.ajax({ | |
url: apiUrl + "?q=" + query.term | |
}).done(function (data) { | |
// data from server is something like | |
// {"Locations":[{"text":"Country","children":[{"id":11107,"text":"UNITED KINGDOM (GB)"}]}]} | |
if (data.Locations && data.Locations.length > 0) { | |
query.callback({ results: data.Locations }); | |
} | |
}); | |
else | |
query.callback(dt); // no search term, display default data | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you explain this more? I'm looking for the exact same solution but unable to follow with my setup. I'm just trying to combine my static data with ajax as a fallback if no match.