JSON format for jquery-select2 multi with ajax

前端 未结 2 571
Happy的楠姐
Happy的楠姐 2020-12-31 03:53

I\'m thinking in moving from Chosen to Select2 because Select2 has native methods for ajax. Ajax is critical because usualy you have to load a lot of data.

I execut

相关标签:
2条回答
  • 2020-12-31 04:28

    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.

    0 讨论(0)
  • 2020-12-31 04:30

    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
      }
    }
    
    0 讨论(0)
提交回复
热议问题