【问题标题】:On removing selected item(using backspace), dropdown list is displaying the removed item only in select2 rails删除所选项目(使用退格)时,下拉列表仅在 select2 rails 中显示已删除的项目
【发布时间】:2020-04-17 08:57:15
【问题描述】:

我在 rails 中有一个多选、select2 下拉菜单。目前,当我从 lis 中选择一些 2-3 个选项时,当我单击退格键时,所选元素将被删除,并且包含所有选项的下拉列表将被显示。但是当我输入一些首字母然后从列表中选择然后单击退格键时,下拉列表正在填充刚刚删除的选项。有没有办法在输入然后选择时,下拉列表将在单击退格键时显示完整列表? 我使用的代码是(根据https://github.com/select2/select2/issues/3354#issuecomment-132389291):

$.fn.select2.amd.require(['select2/selection/search'], function (Search) {
  var oldRemoveChoice = Search.prototype.searchRemoveChoice;
  Search.prototype.searchRemoveChoice = function () {
    oldRemoveChoice.apply(this, arguments);
    this.$search.val('');
  };

  $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
    $('#reports_employee_id').select2({
      matcher: oldMatcher(matchStart),
      templateResult: formatSearch,
      templateSelection: formatSelected,
      maximumSelectionLength: 25,
      placeholder: " "
    })
  });
});

提前致谢!!

【问题讨论】:

    标签: javascript html ruby-on-rails jquery-select2


    【解决方案1】:

    好的,我理解这个问题。我需要处理更改。下面我附上更新的代码以供参考。

    $.fn.select2.amd.require(['select2/selection/search'], function (Search) {
      var oldRemoveChoice = Search.prototype.searchRemoveChoice;
      Search.prototype.searchRemoveChoice = function () {
        oldRemoveChoice.apply(this, arguments);
        this.$search.val('');
        this.handleSearch();
      };
    
      $.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
        $('#reports_employee_id').select2({
          matcher: oldMatcher(matchStart),
          templateResult: formatSearch,
          templateSelection: formatSelected,
          maximumSelectionLength: 25,
          placeholder: " "
        })
      });
    });
    

    只需要添加这一行:

    this.handleSearch();
    

    它按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多