javascriptjqueryjquery-select2-3

Select2 doesn't show the initSelection values


the jquery Select2(Version: 3.5.4) plugin doesn't show my initial value which I loaded on the initSelection function of the plugin. Here is my code:

$("#materialFieldTags").select2({
            tags: true,
            initSelection : function (element, callback) {
                console.log(element);
                console.log(callback);
                var countryId = "3"; //Your values that somehow you parsed them
                var countryText = "mater3";
                var data = [];//Array                   
                var tempJSONMat = {
                    materials: []
                };
                $.ajax({
                    url: "php/FormProcessing.php",
                    type: "post",
                    data: "main=" + "materialFault" + "&faultid="+ main.faultId,
                    dataType: 'json',
                    success: function(data){
                        data.forEach(function(column) {
                            //console.log(column);                
                            tempJSONMat.materials.push({ 
                                "id" : column.material_id,
                                "text"  : column.name
                            });                
                        });
                    }
                });
                callback(tempJSONMat.materials[0]);
            },
            ajax: {
                type: "POST",
                url: 'php/FormFilling.php',
                dataType: 'json',
                data: function (params) {                    
                    return "main=" + "allMaterials" + "&searchterm=" + params;
                },
                processResults: function (data, page) {
                    return {
                        results: $.map(data, function (item) {
                            return {
                                text: item.name,
                                id: item.id
                            };
                        })
                    };
                },
                cache: true
            }
        });

Can you take I look at my code? Because I can't see it!! I also have try the:

$("#materialFieldTags").select2("data",mydata);

After the initialization of the plugin, but I am getting the same result.


Solution

  • Finally, I found my mistake! It was define two element by the same id, by mistake! The data appeared on first one and I was looking the second one.