Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  61] [ 1]  / answers: 1 / hits: 19525  / 11 Years ago, tue, august 20, 2013, 12:00:00

With the following html:



<input type='hidden' id='cantseeme'>


I'm having no trouble creating a Select2 control dynamically, and adding my options:



// simplified example
var select2_ary = [];

select2_ary.push({
id : one,
text : one
});
select2_ary.push({
id : two,
text : two
});

$(#cantseeme).select2({
placeholder : Pick a number,
data : select2_ary
});


However, I can't seem to figure out how to add optgroups this way. I found this discussion on github, and this article on a blog, but the former doesn't seem to discuss dynamic optgroup additions and I simply can't make any sense of the latter.



Anyone have any ideas?


More From » jquery

 Answers
3

I found the answer buried inside the github discussion you linked to, the object structure for optgroups is as follows:



{
id : 1,
text : 'optgroup',
children: [{
id : 2,
text: 'child1'
},
{
id : 3,
text : 'nested optgroup',
children: [...]
}]
}


Demo fiddle


[#76252] Monday, August 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tylerdamiena

Total Points: 139
Total Questions: 90
Total Answers: 118

Location: Liechtenstein
Member since Wed, Dec 8, 2021
3 Years ago
tylerdamiena questions
;