Editor plug-in Select2: not initialize from Ajax response as expected
Editor plug-in Select2: not initialize from Ajax response as expected
Hey,
using the licensed Editor plug-in https://editor.datatables.net/plug-ins/field-type/editor.select2, not works as expected to initialize field values from Ajax response. It make no difference to include Select2 version 4.0.2 or version 4.0.5.
Maybe there could be a problem in the "success" function of the "set" method of the plug-in.
Select2 needs set the values as same as the option DOM elements.
So we take a look at conf._input.val( val )...
. The value of val
does not need to match the value of the response, right?
Described in the following example:
Init-Request:
anyendpoint.json?useIsoCodes=1&initialValue=true&value="MV,TR,CU"
Response:
{
"results": [
{
"id": "MV",
"text": "Maldives"
},
{
"id": "TR",
"text": "Turkey"
},
{
"id": "CU",
"text": "Cuba"
}
]
}
The Plug-In sets the values as:
conf._input.val( "MV,TR,CU" )...
The values should be set correctly as follows conf._input.val( ["MV","TR","CU"] )...
.
The option elements set correctly as:
<select id="DTE_Field_disabled_countries" multiple="" tabindex="-1" class="select2-hidden-accessible" aria-hidden="true">
<option value="MV">Maldives</option>
<option value="TR">Turkey</option>
<option value="CU">Cuba</option>
</select>
What's the difference?
The plug-in uses the lambda function of the "addOption" variable of the "success" function to add the option elements.
The function is called by iterating trough the Ajax response, but setting the values for Select2 the plug-in does not uses the Ajax response. It uses the value of the "val" variable, whose value "MV,TR,CU" is.
Suggestion:
Store the value passed to the lambda function of the "addOption" variable in an array and pass this array to conf._input.val()
.
Answers
Hi,
Could you show me your Select2 field's configuration please?
Thanks,
Allan