Hide/Remove and Show/Restore options in a Select2 dropdown box

Deadly 提交于 2019-12-24 04:56:26

问题


I have an issue where I need to hide or remove options from a select2 dropdown when selected. These selections fill a list. I would rather not remove the option because if the user removes it from the freshly populated list then it should return to the dropdown with its place in the order restored. I've tried the following:

.select2-results .select2-disabled,  .select2-results__option[aria-disabled=true] { 
    display: none;
}

$('#optionId').prop('disabled', true);

With the prop being set in the jQuery change block. I just can't get this piece.


回答1:


I have figured it out. If the disable is wrapped like so:

$(#selectId).select2('destroy');
$(#optionId).prop('disabled', true);
$(#selectId).select2();

then the disabled setting will be honored by select2 and the the display none will make the item disappear. To make it reappear, just follow the same steps with the disabled property set to 'false'.



来源:https://stackoverflow.com/questions/44398167/hide-remove-and-show-restore-options-in-a-select2-dropdown-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!