I have select2 version 4.0.10 in my project.
Made new ResultsAdapter decorator, which adds option “Select All” at the top of dropdown with options.
When selecting this option all displayed options will be selected and added to the text field of multiselect.
It works ok on select2 without ajax (using ArrayAdapter), but doesn’t work when options loaded with ajax (using AjaxAdapter), because options of select is not populated with ajax results by default (only li tags added to parent ul).
In case of ArrayAdapter it takes an array and fills the select with options, then draw select2 and takes options from select and convert them to ul li list for dropdown.
In case of AjaxAdapter it just takes result and converts it to ul li list for dropdown. Select element doesn’t contains option nodes it this case.
If select element is not populated with options, then code like this one
does not working, because jQuery will try to find option in select for each option id in val (check method set of valHooks for select in jQuery).
Is this a bug?
As i understand select element can be filled manually with options, like in this case https://github.com/select2/select2/issues/5668#issuecomment-542247489 or in case if some options need to be preselected, like in documentation here https://select2.org/programmatic-control/add-select-clear-items#preselecting-options-in-an-remotely-sourced-ajax-select2, but fixed this with additional methods in my custom adapter.