How to remove selected option from the option list in select2 multiselect and show selected options in the order they are selected

前端 未结 7 1286
一生所求
一生所求 2021-02-02 08:19

I have select2 multi select field in my form where I want to remove the selected option from the dropdown list after it is selected and again add it to the list if it is removed

7条回答
  •  深忆病人
    2021-02-02 08:41

    Another approach to hide the selected values, is to use the dropdown templateResult option to return null when the value is marked as selected.

    function hideSelected(value) {
      if (value && !value.selected) {
        return $('' + value.text + '');
      }
    }
    
    $(document).ready(function() {
      $('#dynamicAttributes').select2({
        allowClear: true,
        placeholder: {
          id: "",
          placeholder: "Leave blank to ..."
        },
        minimumResultsForSearch: -1,
        width: 600,
        templateResult: hideSelected,
      });
    });
    
    
    
    
    

提交回复
热议问题