How to programmatically inject search queries into Select2 v4?

前端 未结 5 1082
刺人心
刺人心 2020-12-14 16:17

I\'ve built a web-app using a Select2 search box, which connects to our backend via AJAX. The search box passes the query (say \"APPLES\") to the backend, which then updates

相关标签:
5条回答
  • 2020-12-14 16:37

    The answers on this page got me almost there. And because others seem to have the same issues, I am sharing how I solved it.

    Some of the comments on Kevin Browns answer asked how to simulate the ENTER after the text has been entered and is highlighted.

    I managed to do that only with a timeout:

    setTimeout(function() { $('.select2-results__option').trigger("mouseup"); }, 500);
    

    So, the full solution would be this:

    $('select').select2();
    
    function select2_search ($el, term) {
      $el.select2('open');
    
      // Get the search box within the dropdown or the selection
      // Dropdown = single, Selection = multiple
      var $search = $el.data('select2').dropdown.$search || $el.data('select2').selection.$search;
      // This is undocumented and may change in the future
    
      $search.val(term);
      $search.trigger('input');
      setTimeout(function() { $('.select2-results__option').trigger("mouseup"); }, 500);
    
    }
    
    $('button').on('click', function () {
      var $select = $($(this).data('target'));
      select2_search($select, 'Arizona');
    });
    
    0 讨论(0)
  • 2020-12-14 16:40

    for me triggering 'keyup' only worked when the user didn't select any option yet. What works every time is to trigger 'input' instead. (only tested on Chrome)

    so based on Kevin Brown's answer:

    $('select').select2();
    
    function select2_search ($el, term) {
      $el.select2('open');
    
      // Get the search box within the dropdown or the selection
      // Dropdown = single, Selection = multiple
      var $search = $el.data('select2').dropdown.$search || $el.data('select2').selection.$search;
      // This is undocumented and may change in the future
    
      $search.val(term);
      $search.trigger('input');
    }
    
    $('button').on('click', function () {
      var $select = $($(this).data('target'));
      select2_search($select, 'Arizona');
    });
    
    0 讨论(0)
  • 2020-12-14 16:41

    As an addition to this answer, we were trying to programmatically add a new 'tag' to a select2 configured for tagging. Instead of triggering a keyup event on the $search box, we had to do the following.

    $search.val(tag);
    $search.closest('.select2-search--inline')
        .trigger($.Event('input', { which: 13 }));
    $search.closest('.select2-selection--multiple')
        .trigger($.Event('keydown', { which: 13 }));
    

    This first fires an event that causes the 'search' module in select2 to add a 'highlighted' result containing the tag text, then the second event causes select2 to fire the 'select' event internally which adds a new tag to the select2 options and may also trigger other stuff, depending on how you've configured it.

    0 讨论(0)
  • 2020-12-14 16:42

    Select2 used to provide a select2('search', 'term') helper method that would have assisted with this, but it was not brought over to Select2 4.0.0.

    There are a couple of ways that this could be done, but in general they all follow the same pattern of steps

    1. Open the Select2 dropdown
    2. Enter the search text into the search box
    3. Trigger the keyboard events necessary for Select2 to start searching (usually input)

    So, right now, in Select2 4.0.0 the code to do that would look like

    $('select').select2();
    
    function select2_search ($el, term) {
      $el.select2('open');
      
      // Get the search box within the dropdown or the selection
      // Dropdown = single, Selection = multiple
      var $search = $el.data('select2').dropdown.$search || $el.data('select2').selection.$search;
      // This is undocumented and may change in the future
      
      $search.val(term);
      $search.trigger('keyup');
    }
    
    $('button').on('click', function () {
      var $select = $($(this).data('target'));
      select2_search($select, 'Arizona');
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>
    
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>
    
    <select style="width: 200px;" id="single">
      <option value="AL">Alabama</option>
      <option value="AK">Alaska</option>
      <option value="AZ">Arizona</option>
    </select>
    
    <button type="button" data-target="#single">Search for 'Arizona'</button>
    <br /><br />
    
    <select style="width: 200px;" id="multiple" multiple="multiple">
      <option value="AL">Alabama</option>
      <option value="AK">Alaska</option>
      <option value="AZ">Arizona</option>
    </select>
    
    <button type="button" data-target="#multiple">Search for 'Arizona'</button>

    While this example does not use AJAX, it will trigger an AJAX request if Select2 is configured for it.

    0 讨论(0)
  • 2020-12-14 16:51

    You can set minimumInputLength to 0 then in the data function you may check params.term length and if it's 0, set the filter value to your predefined search value.

    It's quite silly, but it works ;)

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