Select2 element not being populated with initialValue

Select2 element not being populated with initialValue

hserveirahserveira Posts: 2Questions: 1Answers: 0

I have recently started to use Editor, and I came across an issue while using the Select2 plugin.
I'm defining the Select2 using the code below:

{
    label: 'Category:',
    name: 'FK_Item_ItemCategoryID',
    type: 'select2',    
    opts: {
        multiple: false,
       initialValue:true,
        ajax: {
        url: '/getItemCategoryData',
        dataType: 'json',
        cache: true
        }
    }
}

The server is queried, and returns the following response:

{
  "results": [
    {
      "id": 1,
      "text": "GARMENTS"
    },
    {
      "id": 2,
      "text": "ACCESSORIES"
    }
  ]
}

Which correctly populates the Select2 options.

However, when the Select2 element already has an initial value, the element is not populated, even with the correct response being returned from the server.
This is an example of the data returned by the following ajax call:

/getItemCategoryData?initialValue=true&value="ACCESSORIES"&

{
  "results": [
    {
      "id": 2,
      "text": "ACCESSORIES"
    }
  ]
}

Any idea why the Select2 element is not being populated with that option?

On a side note, I don't understand why the "value" parameter sent to the server is the "text" value from the select. Shouldn't it be the id?

Thanks,

Answers

  • hserveirahserveira Posts: 2Questions: 1Answers: 0

    It turns out that the table had hardcoded values, so what was being sent to the request wasn't the id, but the text. As soon as I fixed that it started working.

    Cheers,

Sign In or Register to comment.