Skip to content Skip to sidebar Skip to footer

Jquery Select2 Json Data With Optgroup And Images

I am using select2 with spring mvc. I got the data from my controller that I need to display here in terms of options. But I want them to be grouped as optgroup and also I want ima

Solution 1:

Select2 maps data objects to <option> and <optgroup> tags using the following logic


A data object (what is returned in the list) that looks like

{
  'id': 'value-here',
  'text': 'text-here'
}

Will be mapped to an <option> that looks like

<option value="value-here">text-here</option>

A data object that looks like

{
  'text': 'label-here',
  'children': [data_object, data_object]
}

Will be mapped into an <optgroup> that looks like

<optgroup label="label-here">
  <!-- HTML for the `children` -->
</optgroup>

So the data object that you are looking to return is

{
  'text': 'group',
  'children': [
    {
      'id': 'imageName',
      'text': 'value 1'
    },
    {
      'id': 'imageName',
      'text': 'value 1'
    }
  ]
}

Post a Comment for "Jquery Select2 Json Data With Optgroup And Images"