Open moysoft opened 4 years ago
I had the same issue and found that select2 is adding the following class select2-results__option--selectable
to options so it can be selected.
By reviewing the js code, I found that class maybe removed for some reasons:
If the options is disabled:
if ((data.element != null && matches.call(data.element, ':disabled')) ||
(data.element == null && data.disabled)) {
attrs['aria-disabled'] = 'true';
option.classList.remove('select2-results__option--selectable');
option.classList.add('select2-results__option--disabled');
}
If id is missing from response (that was my case): select2 expects a "id" key to be returned in results which is being used as the option value that will be sent in post body:
if (data.id == null) {
option.classList.remove('select2-results__option--selectable');
}
So your endpoint response fomat MUST be as the following:
{
"results": [
{
"id": someId,
...
...
},
{
"id": someId2,
...
...
}
]
}
Of course it would be great if there's a way to identify the id key in widget initialization.
If the option is the group heading (For example you're showing cities grouped by country so country name won't be selectable):
if (data.children) {
attrs.role = 'group';
attrs['aria-label'] = data.text;
option.classList.remove('select2-results__option--selectable');
option.classList.add('select2-results__option--group');
}
Steps to reproduce the issue
In view
in Controller :
Expected behavior and actual behavior
When I follow those steps, I see...
https://prnt.sc/rwykv9
I can't select the list, and the list background color is yellow
I was expecting... I can select the list, and the list background color is white
Environment
Browsers
Operating System
Libraries