5
votes

When you select a specific item in the dropdown, custom selection is hidden. Why? In my options I have closeOnSelect: false;

Warning! I use $.fn.select2.amd.require. 'select2/dropdown/closeOnSelect' Cannot be deleted.

$(function() {
  $.fn.select2.amd.require([
      "select2/utils",
      "select2/dropdown",
      "select2/dropdown/attachContainer",
      'select2/dropdown/closeOnSelect'
  ], function (Utils, DropdownAdapter, AttachContainer, closeOnSelect) {
      $('select').select2({
          dropdownAdapter: Utils.Decorate(Utils.Decorate(DropdownAdapter, AttachContainer), closeOnSelect),
          closeOnSelect: false
      });
  });
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.js"></script>
<div class="select i-select">
  <select name="" id="">
    <option value="1">White</option>
    <option value="2">Gray light</option>
    <option value="3">Gray</option>
    <option value="4">Black</option>
  </select>
</div>
1
Read the docs: Note that this option is only applicable to multi-select controls. select2.org/…Rory McCrossan
No it works for single selection also check my answerBasil

1 Answers

2
votes

You can set the closeOnSelect to false before using it with the following code

closeOnSelect= function(){return false;};

$(function() {
  $.fn.select2.amd.require([
      "select2/utils",
      "select2/dropdown",
      "select2/dropdown/attachContainer",
      'select2/dropdown/closeOnSelect'
  ], function (Utils, DropdownAdapter, AttachContainer, closeOnSelect) {
  
      closeOnSelect= function(){return false;};
      
      $('select').select2({
          dropdownAdapter: Utils.Decorate(Utils.Decorate(DropdownAdapter, AttachContainer), closeOnSelect)
      });
  });
});
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.12/js/select2.full.js"></script>
<div class="select i-select">
  <select name="" id="">
    <option value="1">White</option>
    <option value="2">Gray light</option>
    <option value="3">Gray</option>
    <option value="4">Black</option>
  </select>
</div>