javascript - Jquery select2 json data with optgroup and images -


i using select2 spring mvc. got data controller need display here in terms of options. want them grouped optgroup , want images appended in can inserted manually given below : -

 <optgroup label="group">     <option value="imagename">value 1</option>     <option value="imagename">value 1</option>     </optgroup> 

where imagename name of image. want : 1) group options in json. 2) provide image attribute in json select2 can form data.

here code :

$("#my-select").select2({         data : [ {             id : 0,             text : 'enhancement'         }, {             id : 1,             text : 'bug'         }, {             id : 2,             text : 'duplicate'         }, {             id : 3,             text : 'invalid'         }, {             id : 4,             text : 'wontfix'         } ]     }); 

i creating json manually objects. can provide data here. suggestions ?

select2 maps data objects <option> , <optgroup> tags using following logic


a data object (what returned in list) looks like

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

will mapped <option> looks like

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

a data object looks like

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

will mapped <optgroup> looks like

<optgroup label="label-here">   <!-- html `children` --> </optgroup> 

so data object looking return is

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

Comments