Jquery select2 json数据与optgroup和图像 [英] Jquery select2 json data with optgroup and images

查看:121
本文介绍了Jquery select2 json数据与optgroup和图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有spring mvc的select2。我从控制器获得了数据,我需要在选项方面显示。但我希望它们被分组为optgroup,并且我希望图像可以附加在其中,可以手动插入,如下所示: -

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 images to be appended in it which can be inserted manually as given below : -

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

其中imageName是图像的名称。我想:
1)json中的组选项。
2)在json中提供此图像属性,以便select2可以形成数据。

Where imageName is the name of image. I want to : 1) Group options in json. 2) Provide this image attribute in json so that select2 can form the data.

以下是代码:

$("#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。所以我可以在这里提供任何数据。有什么建议?

I am creating my json manually from my objects. So I can provide any data here. Any suggestions ?

推荐答案

Select2将数据对象映射到< option> < optgroup> 标签使用以下逻辑

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

A数据对象(列表中返回的内容)看起来像

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

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

将映射到< option> ,看起来像

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






一个看起来像


A data object that looks like

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

将映射到 < optgroup> 看起来像

<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'
    }
  ]
}

这篇关于Jquery select2 json数据与optgroup和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆