Selecting value from ajax-loaded data does not work


#1

Hi!

Firstly, thank you for the great plugin!

So, the problem:

i have an select with custom data loading:

formWrapper.find(’.select-storage-holder’).select2({
ajax: {
transport: function (params, success, failure) {

                    var o  = {
                        command: 'getAllStorages',
                        object: 'project',
                        params: {
                            id: formInstance.data.data[0].project_id
                        }
                    };

                    socketQuery(o ,function(res){

                        if(!res.code == 0){
                            toastr[res.toastr.type](res.toastr.message);
                            return false;
                        }

                        var sdata = {
                            results: [{
                                text: "Parental storages",
                                children: []
                            },{
                                text: "Storages",
                                children: []
                            }],
                            paginate: {
                                more: true
                            }
                        };

                        for(var i in res.parent_storage){
                            sdata.results[0].children.push({
                                id: res.parent_storage[i].id,
                                text: res.parent_storage[i].name
                            });
                        }

                        for(var k in res.storage){
                            sdata.results[1].children.push({
                                id: res.storage[k].id,
                                text: res.storage[k].name
                            })
                        }

                        console.log(sdata);

                        success(sdata);

                    });

                }
            }
        });

Data in console before adding to select (converted to JSON):

{“results”:[{“text”:“Parental storages”,“children”:[{“id”:7,“text”:“Storage in TheGreatProject”,“selected”:true}]},{“text”:“Storages”,“children”:[{“id”:8,“text”:“storage in test7”}]}],“paginate”:{“more”:true}}


Dropdown works correctly, but when i try to select some value it does not works.

Please help=)


#2

You haven’t shown the code where you actually initialize the select2 widget, but I assume you’re assigning the sdata object as the select2 widget’s data property when you initialize your widget.

Assuming that’s true, I’m guessing that when you say the dropdown works correctly, you mean that you can click in the select2 widget that’s rendered on the page and see the expected data. But what do you mean when you say it doesn’t work when you select a value? What exactly happens after you select an item from the dropdown?

Can you provide a code sample?