Select2: Hide certain options dynamically

前端 未结 10 771
情深已故
情深已故 2020-12-03 06:56

Basically what I\'m looking for is the ability to hide options from the dropdown of select items. So, technically they would still be options, but you just wouldn\'t be able

10条回答
  •  [愿得一人]
    2020-12-03 07:13

    I just made a variation of he answer 6 and changed class by hidden attribute, so this way every option with hidden attribute will also be hidden in a select2 container like below that the option 1 will be hidden:

    
    
    $('.mySelect').select2({templateResult: hideSelect2Option});
    
    function hideSelect2Option(data, container) {
        if(data.element) {
            $(container).addClass($(data.element).attr("class"));
            $(container).attr('hidden',$(data.element).attr("hidden"));
        }
        return data.text;
    }
    
       
    

提交回复
热议问题