在Select2中动态添加选项和选项组
Dynamically adding options and optgroups in Select2
使用以下html:
<input type='hidden' id='cantseeme'>
我在动态创建Select2控件并添加我的选项时没有遇到任何问题:
// 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
});
然而,我似乎不知道如何以这种方式添加optgroups
。我在github上找到了这个讨论,在博客上也找到了这篇文章,但前者似乎没有讨论动态optgroup
添加,我根本无法理解后者。
有人有什么想法吗?
我在你链接的github讨论中找到了答案,optgroups
的对象结构如下:
{
id : 1,
text : 'optgroup',
children: [{
id : 2,
text: 'child1'
},
{
id : 3,
text : 'nested optgroup',
children: [...]
}]
}
演示小提琴
// `Header One` Is Selectable
[{
id : 0,
text : "Header One",
children : [{
id : 0,
text : "Item One"
}, {
...
}]
}]
// `Header One` Is Not Selectable
[{
text : "Header One",
children : [{
id : 0,
text : "Item One"
}, {
...
}]
}]
相关文章:
- 如何为下拉列表的每个选项添加一个属性
- 使用jQuery向“选择选项”添加默认属性
- 将默认选项添加到ng-if
- 从jquery中的select选项添加两个不同的数组值(text&value)
- 为selectize.js选项添加数据属性
- 将select选项添加到id中
- AngularJS ng选项添加了另一个选项
- 将选项添加到插件msDropdown生成的下拉列表的最佳方式是什么
- 将选项添加到多选并设置为选定
- 如何将空白选项添加到木偶收藏视图生成的选择中
- 如何将此选项添加到此图像选取器项目中
- 使用 XHR 更新服务器文件并将选项添加到 html 选择
- 如何使用 JavaScript 将选项添加到 HTML 表单下拉列表中
- 为什么当我将数据选项添加到骨干模型获取时,它不会触发回调
- 如何将下拉列表中的所有选项添加到 php 中的数组中
- 角度JS多选选项添加额外的?选择
- 自动选择<选项>添加到<选择>
- 如何动态地将所选选项添加到表中
- 动态地将选项添加到 Angular 选定的组合框中
- AngularJS:将选项添加到选择而不选择它