Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initSelection is not getting called in select2 jquery plugin

I am using select2 jquery plugin from http://ivaynberg.github.io/select2/.

I am using following code.

 $(document).ready(function () {
            $("#e6").select2({
                placeholder: "Search for a movie",
                minimumInputLength: 1,
                ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
                    url: "http://api.rottentomatoes.com/api/public/v1.0/movies.json",
                    dataType: 'jsonp',
                    quietMillis: 1000,
                    data: function (term, page) {
                        return {
                            q: term, // search term
                            page_limit: 10,
                            apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working
                        };
                    },
                    results: function (data, page) { // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to alter remote JSON data
                        return { results: data.movies };
                    }
                },
                initSelection: function (element, callback) {
                    var id = 9942;//$(element).val();
                    alert('initSelection');
                    if (id !== "") {
                        $.ajax("http://api.rottentomatoes.com/api/public/v1.0/movies/" + id + ".json", {
                            data: {
                                apikey: "ju6z9mjyajq2djue3gbvv26t"
                            },
                            dataType: "jsonp"
                        }).done(function (data) { callback(data); });
                    }
                },

                formatResult: movieFormatResult, // omitted for brevity, see the source of this page
                formatSelection: movieFormatSelection,  // omitted for brevity, see the source of this page
                dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
                escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
            });

        });


        $(document).ready(function () {
            $("#e6").on("select2-selecting", function (e) {
                var v = 10;
                alert("selecting val=" + e.val + " choice=" + JSON.stringify(e.choice));
                var id = document.getElementById('<%= savebtn.ClientID %>');
                id.value = e.val;
                id.click();

            });
        });

Problem: For some reasons initSelection is not getting called and because of this, I am not able to set value for the textbox across post-backs.

I am using loading remote data example from http://ivaynberg.github.io/select2/ site.

I looked at the documentation for initSelection and it says "This function will only be called when there is initial input to be processed.", I am not sure what exactly it means.

Am I doing something wrong? Please help

like image 973
SharpCoder Avatar asked Aug 15 '13 12:08

SharpCoder


People also ask

What is initSelection in Select2?

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 has been replaced by the current method on the data adapter. AMD Modules: select2/compat/initSelection"

Is Select2 jQuery?

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

How do I assign a value to Select2?

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.

Is Select2 a plugin?

Select2 is a powerful JQuery plugin that is designed to add more power to the traditional <select> box that is displayed on the browser. It supports searching, remote data-sets via AJAX, mult-selects, pagination, grouping, custom rendering and so much more.


1 Answers

You should provide initial value:

<input ... value="192" ... />

or

$('#e6').select2('val', 192);

For multiple values, either:

<input ... value="192,193" ... />

or

$('#e6').select2('val', [192,193]);

See JSFiddle.

like image 79
Mati Avatar answered Oct 02 '22 17:10

Mati