我在spring mvc中使用select2。我从我的控制器获得了需要在这里以选项的形式显示的数据。但我希望它们被分组为optgroup,并且我还希望图像被附加到其中,可以手动插入,如下所示:
<optgroup label="group">
<option value="imageName">value 1</option>
<option value="imageName">value 1</option>
</optgroup>
其中imageName是镜像的名称。我想: 1) json中的分组选项。2)在json中提供此图像属性,以便select2可以形成数据。
代码如下:
$("#my-select").select2({
data : [ {
id : 0,
text : 'enhancement'
}, {
id : 1,
text : 'bug'
}, {
id : 2,
text : 'duplicate'
}, {
id : 3,
text : 'invalid'
}, {
id : 4,
text : 'wontfix'
} ]
});
我正在从我的对象手动创建我的json。所以我可以在这里提供任何数据。有什么建议吗?
发布于 2015-07-18 02:05:56
Select2使用以下逻辑将数据对象映射到<option>
和<optgroup>
标记
一个数据对象(在列表中返回的内容),如下所示
{
'id': 'value-here',
'text': 'text-here'
}
将被映射到如下所示的<option>
<option value="value-here">text-here</option>
一个数据对象,看起来像
{
'text': 'label-here',
'children': [data_object, data_object]
}
将被映射到一个<optgroup>
,它看起来像
<optgroup label="label-here">
<!-- HTML for the `children` -->
</optgroup>
因此,您希望返回的数据对象是
{
'text': 'group',
'children': [
{
'id': 'imageName',
'text': 'value 1'
},
{
'id': 'imageName',
'text': 'value 1'
}
]
}
https://stackoverflow.com/questions/31452318
复制相似问题