Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the initial value of Select2 when using AJAX?

I have a select2 v4.0.0 being populated from an Ajax array. If I set the val of the select2 I can see via javascript debugging that it has selected the correct item (#3 in my case), however this is not shown in the select box, it is still showing the placeholder. enter image description here

Whereas I should be seeing something like this: enter image description here

In my form fields:

<input name="creditor_id" type="hidden" value="3"> <div class="form-group minimal form-gap-after">     <span class="col-xs-3 control-label minimal">         <label for="Creditor:">Creditor:</label>     </span>     <div class="col-xs-9">         <div class="input-group col-xs-8 pull-left select2-bootstrap-prepend">             <select class="creditor_select2 input-xlarge form-control minimal select2 col-xs-8">                 <option></option>             </select>         </div>     </div> </div> 

My javascript:

var initial_creditor_id = "3"; $(".creditor_select2").select2({     ajax: {        url: "/admin/api/transactions/creditorlist",        dataType: 'json',        delay: 250,        data: function (params) {            return {                 q: params.term,                 c_id: initial_creditor_id,                 page: params.page            };        },        processResults: function (data, page) {             return {                 results: data             };        },        cache: true    },    placeholder: "Search for a Creditor",    width: "element",    theme: "bootstrap",    allowClear: true    }).on("select2:select", function (e) {       var selected = e.params.data;       if (typeof selected !== "undefined") {            $("[name='creditor_id']").val(selected.creditor_id);            $("#allocationsDiv").hide();            $("[name='amount_cash']").val("");            $("[name='amount_cheque']").val("");            $("[name='amount_direct']").val("");            $("[name='amount_creditcard']").val("");         }     }).on("select2:unselecting", function (e) {         $("form").each(function () {             this.reset()         });         ("#allocationsDiv").hide();         $("[name='creditor_id']").val("");     }).val(initial_creditor_id); 

How can I make the select box show the selected item rather than the placeholder? Should I be sending this as part of the AJAX JSON response perhaps?

In the past, Select2 required an option called initSelection that was defined whenever a custom data source was being used, allowing for the initial selection for the component to be determined. This worked fine for me in v3.5.

like image 396
Greg Avatar asked May 19 '15 04:05

Greg


People also ask

How do I set Select2 values?

To set selected value of jQuery Select2 with JavaScript, we call val and then trigger . Then we set its value with val to the option with value 0. Finally, we call trigger with 'change' to update the drop down to show the value we set.

How do I initiate Select2?

Select2 will register itself as a jQuery function if you use any of the distribution builds, so you can call . select2() on any jQuery selector where you would like to initialize Select2. // In your Javascript (external . js resource or <script> tag) $(document).

How do I create a Select2 dynamically?

Select2 jQuery plugin is easy to add on the <select > element. Need to call the select2() method on the selector to initialize. If you adding select2 on a class or select element and when you add an element dynamically then select2 is not initialized on that element.

How do I empty Select2 value?

In order to clear the selection of those values which are selected using a Select2 drop down,we can use the empty() function. The dropdown can be reset using Jquery. $("#select2_example"). empty();


1 Answers

You are doing most things correctly, it looks like the only problem you are hitting is that you are not triggering the change method after you are setting the new value. Without a change event, Select2 cannot know that the underlying value has changed so it will only display the placeholder. Changing your last part to

.val(initial_creditor_id).trigger('change'); 

Should fix your issue, and you should see the UI update right away.


This is assuming that you have an <option> already that has a value of initial_creditor_id. If you do not Select2, and the browser, will not actually be able to change the value, as there is no option to switch to, and Select2 will not detect the new value. I noticed that your <select> only contains a single option, the one for the placeholder, which means that you will need to create the new <option> manually.

var $option = $("<option selected></option>").val(initial_creditor_id).text("Whatever Select2 should display"); 

And then append it to the <select> that you initialized Select2 on. You may need to get the text from an external source, which is where initSelection used to come into play, which is still possible with Select2 4.0.0. Like a standard select, this means you are going to have to make the AJAX request to retrieve the value and then set the <option> text on the fly to adjust.

var $select = $('.creditor_select2');  $select.select2(/* ... */); // initialize Select2 and any events  var $option = $('<option selected>Loading...</option>').val(initial_creditor_id);  $select.append($option).trigger('change'); // append the option and update Select2  $.ajax({ // make the request for the selected data object   type: 'GET',   url: '/api/for/single/creditor/' + initial_creditor_id,   dataType: 'json' }).then(function (data) {   // Here we should have the data object   $option.text(data.text).val(data.id); // update the text that is displayed (and maybe even the value)   $option.removeData(); // remove any caching data that might be associated   $select.trigger('change'); // notify JavaScript components of possible changes }); 

While this may look like a lot of code, this is exactly how you would do it for non-Select2 select boxes to ensure that all changes were made.

like image 81
Kevin Brown-Silva Avatar answered Sep 24 '22 21:09

Kevin Brown-Silva