select2 - hiding the search box

前端 未结 18 1704
太阳男子
太阳男子 2021-01-29 19:04

For my more significant selects, the search box in Select2 is wonderful. However, in one instance, I have a simple selection of 4 hard-coded choices. In this case, the search bo

相关标签:
18条回答
  • 2021-01-29 19:21

    It's on their Examples page: https://select2.org/searching#hiding-the-search-box

    $(".js-example-basic-hide-search").select2({
      minimumResultsForSearch: Infinity
    });
    
    0 讨论(0)
  • 2021-01-29 19:21

    If you want to hide on initial opening and you are populating the dropdown via ajax call, add the following to the ajax block in your select2 declaration:

    beforeSend: function () 
      {
        $('.select2-search--dropdown').addClass('hidden');
      }
    

    To then show it again (and focus) after your ajax request is successful:

      success: function() {
          $('.select2-search--dropdown').removeClass('select2-search--hide'); // show search bar then focus
          $('.select2-search__field')[0].focus();
      }
    
    0 讨论(0)
  • 2021-01-29 19:22

    This is the best solution, clean and work good :

    $("#select2Id").select2 () ;
    $("#select2Id").select2 ('container').find ('.select2-search').addClass ('hidden') ;
    

    Then, create a class .hidden { display;none; }

    0 讨论(0)
  • 2021-01-29 19:28
    //readonly on all select2 input
    $(".select2-search input").prop("readonly", true);
    
    0 讨论(0)
  • 2021-01-29 19:28

    For multiselect you have to write js code, there is no settings property.

    $('#js-example-basic-hide-search-multi').select2();
    
    $('#js-example-basic-hide-search-multi').on('select2:opening select2:closing', function( event ) {
        var $searchfield = $(this).parent().find('.select2-search__field');
        $searchfield.prop('disabled', true);
    });
    

    This mentioned on their page: https://select2.org/searching#multi-select

    0 讨论(0)
  • 2021-01-29 19:29

    Version 4.0.3

    Try not to mix user interface requirements with your JavaScript code.

    You can hide the search box in the markup with the attribute:

    data-minimum-results-for-search="Infinity"

    Markup

    <select class="select2" data-minimum-results-for-search="Infinity"></select>
    

    Example

    $(document).ready(function() {
      $(".select2").select2();
    });
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
    
    <label>without search box</label>
    <select class="select2" data-width="100%" data-minimum-results-for-search="Infinity">
      <option>one</option>
      <option>two</option>
    </select>
    
    <label>with search box</label>
    <select class="select2" data-width="100%">
      <option>one</option>
      <option>two</option>
    </select>

    0 讨论(0)
提交回复
热议问题