Select2 автоматически выбирает элемент для запроса ajaxJquery

Программирование на jquery
Ответить
Anonymous
 Select2 автоматически выбирает элемент для запроса ajax

Сообщение Anonymous »


is there any way to make select2 auto select an item on ajax request when an specific condition matches?

The below is my current code:

const optionFormatSelect = (item) => { if (!item.id) { return item.text; } var span = document.createElement('span'); var template = ''; template += ''; template += '' template += '' + item.name + ''; // Access the data attributes directly from item if (item.email) { template += '' + item.email + ''; } if (item.phone) { template += '' + item.phone + ''; } template += ''; template += ''; span.innerHTML = template; return $(span); } function populateOwnerSelect(owner_id = null) { var ownerSelect = $('#owner_id'); ownerSelect.empty(); ownerSelect.select2({ placeholder: "Select an owner", dropdownParent: $("#editModal"), ajax: { url: '{{ route('owners.getList') }}', dataType: 'json', delay: 250, data: function (params) { if (owner_id !== null && params.term === undefined) { return { id: owner_id, }; } else { return { q: params.term, }; } }, processResults: function (data, params) { return { results: data.results, }; }, cache: true }, escapeMarkup: function(markup) { return markup; }, templateSelection: optionFormatSelect, templateResult: optionFormatSelect }); if (owner_id !== null) { ownerSelect.select2('open'); ownerSelect.val(owner_id).trigger('change'); } } and the below i am calling in my another function to make the option selected:

populateOwnerSelect(listing.owner_id); But it only opens the dropdown but it do not make the option selected, please note I've added the select2 open trigger to just identify the issue as i was thinking the options are only listed once user opens the dropdown. But it still not making the option selected. Where I am wrong can anyone pelase check the code?


Источник: https://stackoverflow.com/questions/781 ... ax-request
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Jquery»