0

When i geve to Select 2 a remote Ajax (with groups) the 'selected' option is not set in aria-selected , that load only ID and text but no selected option.

{
   "results":[
      {
         "text":"User",
         "children":[
            {
               "id":37,
               "text":"Victor",
               "selected":true
            },
            {
               "id":153,
               "text":"Manuel",
               "selected":false
            }
         ]
      },
      {
         "text":"Support",
         "children":[
            {
               "id":39,
               "text":"Robert",
               "selected":true
            },
            {
               "id":36,
               "text":"Rudolf",
               "selected":false
            },
            {
               "id":202,
               "text":"Claus",
               "selected":false
            }
         ]
      }
   ]
}

This is my Javascript:

$('#my_select2').select2({
                    allowClear: true,
                    minimumInputLength: 0,
                    maximumSelectionLength: 0,
                    multiple: true,
                    closeOnSelect: true,
                    delay: 250,
                    ajax: {
                            type: "POST",
                            url: "my_json.json",
                            dataType : 'json',                            
                            cache: false                         
                          },
                  });

i read that i need process the result but when i put this code then i dont have results ...

processResults: function (data, params) {
                                return {
                                  results: jQuery.map( data.children, function( children ) {
                                    var arr = {id:children.id,text:children.text};
                                    return arr;
                                  }),
                                };
                              }
victorelec14
  • 178
  • 12
  • Did you try `$('#my_select2').select2({...}).trigger("change")`? Also related: [Programmatic selection of select2 which retrieves its data via Ajax](https://stackoverflow.com/questions/29704873) and [Select2 Ajax Method Not Selecting](https://stackoverflow.com/q/14819865) – Fr0zenFyr Mar 21 '18 at 11:42
  • @Fr0zenFyr yes but same result .... – victorelec14 Mar 21 '18 at 12:36
  • Please share a [fiddle](https://jsfiddle.net/)... I may be able to help – Fr0zenFyr Mar 22 '18 at 10:51

0 Answers0