Load values in select2 multiselect
Asked Answered
A

2

7

I'm using select2 in place of search box.

Here i'm using to load countries values like this

$("#countries").select2({
    multiple: true,
    tags:["India", "Japan", "Australia","Singapore"],
    tokenSeparators: [","]
});

When i press save button, they are properly submitted to the server, now here the question is when i want to modify the country field after saving to server, how i can load the saved values to the country field.

This is how i retrieve data from server

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {

    //the opts here contains the json values of the countries.

    //what code should be written here to load the values in $('#countries).select2();

    //user can add some more countries or can remove the previously added countries. 

}
Arianearianie answered 15/4, 2013 at 8:9 Comment(0)
A
4

I just use this http://ivaynberg.github.io/select2/#event_ext_change link and use the trigger function to load the values

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {
        parent.parent.parent.showLoader();
        if (opts) {
            $("#countries").val(opts).trigger("change");
        } 

This trick load the value in the select box.

Arianearianie answered 15/4, 2013 at 10:56 Comment(1)
I know this is old so forgive me but this doesn't seem to work with a 2 dimensional array? I can load values such as ('dog','cat','mouse') but not ('1': 'Dog', '2':'Cat', '3':'Mouse'). Were you able to do that?Maritime
G
8

Please have a look a documentation under section Loading Remote Data.

You will find and example like:

$("#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"
          // Other stuffs
          }
});

Also you can do like this:

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts){
    $("#countries").select2({
        data: opts
    });
})

Your JSON data must be in format like below:

[{id:0,text:'enhancement'},
 {id:1,text:'bug'},
 {id:2,text:'duplicate'},
 {id:3,text:'invalid'},
 {id:4,text:'wontfix'}
]
Groves answered 15/4, 2013 at 8:20 Comment(2)
I believe that the data retrieved from the url is loaded into search box on a user event. Like user types input then value is retrieved and he selects the value. What i want here is to load all the values retrieved from json without any user event. @Kishor SubediArianearianie
Thanks @Kishor for the help, but i found more easier way to do this, i just used trigger function to load the values.Arianearianie
A
4

I just use this http://ivaynberg.github.io/select2/#event_ext_change link and use the trigger function to load the values

$.getJSON('/dataprovider?data=fetchCountriesForm', function(opts) {
        parent.parent.parent.showLoader();
        if (opts) {
            $("#countries").val(opts).trigger("change");
        } 

This trick load the value in the select box.

Arianearianie answered 15/4, 2013 at 10:56 Comment(1)
I know this is old so forgive me but this doesn't seem to work with a 2 dimensional array? I can load values such as ('dog','cat','mouse') but not ('1': 'Dog', '2':'Cat', '3':'Mouse'). Were you able to do that?Maritime

© 2022 - 2024 — McMap. All rights reserved.