JSON format for jquery-select2 multi with ajax

我与影子孤独终老i 提交于 2019-11-30 08:18:27
igor.vaynberg

If you switched to JSON make sure you switch dataType to JSON from JSONP.

The format is specified here: http://ivaynberg.github.io/select2/#doc-query

The JSON should look like this:

{results: [choice1, choice2, ...], more: true/false }

Basically the only required attribute in the choice is the id. if the option contains other child options (such as in case of optgroup-like choices) then those are specified inside the children attribute.

The default choice and selection renderer expect a text attribute in every choice - that's what is used to render the choice.

so a complete example of a US state using default renderer might look like this:

{
    "results": [
        {
            "id": "CA",
            "text": "California"
        },
        {
            "id": "CO",
            "text": "Colarado"
        }
    ],
    "more": false
}

Hope this gets you started.

Official documentation for required JSON format: ref. https://select2.org/data-sources/formats

{
  "results": [
    {
      "id": 1,
      "text": "Option 1"
    },
    {
      "id": 2,
      "text": "Option 2"
    }
  ],
  "pagination": {
    "more": true
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!