How to use Select2 with JSON via Ajax request?

for select2 v4.0.0 slightly different $(“.itemSearch”).select2({ tags: true, multiple: true, tokenSeparators: [‘,’, ‘ ‘], minimumInputLength: 2, minimumResultsForSearch: 10, ajax: { url: URL, dataType: “json”, type: “GET”, data: function (params) { var queryParameters = { term: params.term } return queryParameters; }, processResults: function (data) { return { results: $.map(data, function (item) { return { text: item.tag_value, … Read more

Update select2 data without rebuilding the control

select2 v3.x If you have local array with options (received by ajax call), i think you should use data parameter as function returning results for select box: var pills = [{id:0, text: “red”}, {id:1, text: “blue”}]; $(‘#selectpill’).select2({ placeholder: “Select a pill”, data: function() { return {results: pills}; } }); $(‘#uppercase’).click(function() { $.each(pills, function(idx, val) { … Read more

How to set selected value of jQuery Select2?

SELECT2 < V4 Step #1: HTML <input name=”mySelect2″ type=”hidden” id=”mySelect2″> Step #2: Create an instance of Select2 $(“#mySelect2”).select2({ placeholder: “My Select 2”, multiple: false, minimumInputLength: 1, ajax: { url: “/elements/all”, dataType: ‘json’, quietMillis: 250, data: function(term, page) { return { q: term, }; }, results: function(data, page) { return {results: data}; }, cache: true }, … Read more

Select2 doesn’t work when embedded in a bootstrap modal

Ok, I’ve got it to work. change <div id=”myModal” class=”modal hide fade” tabindex=”-1″ role=”dialog” aria-labelledby=”myModalLabel” aria-hidden=”true”> <div class=”modal-header”> <button type=”button” class=”close” data-dismiss=”modal” aria-hidden=”true”>×</button> <h3 id=”myModalLabel”>Panel</h3> </div> <div class=”modal-body” style=”max-height: 800px”> to <div id=”myModal” class=”modal hide fade” role=”dialog” aria-labelledby=”myModalLabel” aria-hidden=”true”> <div class=”modal-header”> <button type=”button” class=”close” data-dismiss=”modal” aria-hidden=”true”>×</button> <h3 id=”myModalLabel”>Panel</h3> </div> <div class=”modal-body” style=”max-height: 800px”> (remove tabindex=”-1″ … Read more