I have next html structure
<select class="change_item_dropdown_ajax form-control" id="item_id" name="item_id" onchange="updateData(this, this.value, 16)" >
<optgroup label="System Data">
<option value="17">Test</option>
<option selected="selected" value="18">System</option>
</optgroup>
</select>
Javascript
$(".change_item_dropdown_ajax").select2({
ajax: {
url: "get_user_items",
dataType: 'json',
delay: 250,
theme: "classic",
data: function (params) {
return {
q: { name_contains: params.term } // search term
};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
allowClear: true,
placeholder: '--- Please select item ---',
minimumInputLength: 1
});
I want to make possibility for customer to see some default system options with items <optgroup label="System Data">
, but also add ability to make search ajax queries by his own items.
However after binding select2 it doesn't show <optgroup label="System Data">...</optgroup>
,
select2 options are empty and it only displays hint "Please enter 1 or more characters".
It's not even clear if it is possible to do, thanks.
UPD Related to select2 removes default options when using ajax
Select-2 removes options when using ajax adapter.
UPD2 github issue https://github.com/select2/select2/issues/3828