Skip to content Skip to sidebar Skip to footer

JSON Format For Jquery-select2 Multi With Ajax

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 executed sucessfully t

Solution 1:

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.


Solution 2:

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

Post a Comment for "JSON Format For Jquery-select2 Multi With Ajax"