【问题标题】:Select2 optgroup group on JSON/Ajax format requestJSON/Ajax 格式请求上的 Select2 optgroup 组
【发布时间】:2016-12-10 08:25:29
【问题描述】:

将 Select2 与 JSON/Ajax 调用一起使用,我正在尝试创建这样的组:

<optgroup label="Property Reference">
     <option value="1">5742</option> 
     <option value="2">5788</option>
     <option value="3">5762</option>
     <option value="4">5711</option>
</optgroup>

我的问题is not available 的文档。


我设法开始工作的格式:

{
    results: [
        {
            id: 'CA',
            text: 'California'
        },
        {
            id: 'CO',
            text: 'Colarado'
        ]
    }
}

但是这里没有组。

按照this question,我尝试了以下格式:

{
    "results": {
        "text": "Mountain Time Zone",
        "children": [
            {
                "id": "CA",
                "text": "California"
            },
            {
                "id": "CO",
                "text": "Colorado"
            }
        ]
    }
}

{
    "results": {
        "Mountain Time Zone": [
            {
                "id": "CA",
                "text": "California"
            },
            {
                "id": "CO",
                "text": "Colorado"
            }
        ]
    }
}

{
    "Mountain Time Zone": [
        {
            "id": "CA",
            "text": "California"
        },
        {
            "id": "CO",
            "text": "Colorado"
        }
    ]
}

但没有一个工作。有谁知道正确的格式是什么?

你可以在这里测试它:https://jsfiddle.net/5am4zda6/2/

编辑已解决:该死...忘记[]。 正确的格式是:

{
    "results": [
        {
            "text": "Mountain Time Zone",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        }
    ]
}

【问题讨论】:

  • 它在小提琴上不起作用。

标签: jquery json ajax jquery-select2 select2


【解决方案1】:

试试这样:

{
    "results": [
        {
            "text": "Groupe 1",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        },
        {
            "text": "Groupe 2",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        }
    ]
}

我想你在“结果”之后忘记了[juste

【讨论】:

  • 如何使用 group ajax 数据进行 select2 分页?
【解决方案2】:

我之前没有尝试过这个插件,但是在查看了您提到的相关问题之后,尝试通过 "data" 更改 "result" 索引像这样:

{
    "data" : {
        'text': 'Mountain Time Zone',
        'children': [
          {
            'id': 'CA',
            'text': 'California'
          },
          {
            'id': 'CO',
            'text': 'Colorado'
          }
        ]
    }
}

但如果你能提供给我们一个工作的 jsfiddle 会更好。

【讨论】:

  • 感谢您的回答。不幸的是,它也不起作用......我正在尝试设置一个 jsfiddle。
猜你喜欢
  • 2018-08-18
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 2020-04-14
  • 2013-05-12
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
相关资源
最近更新 更多