在删除所选项目(使用退格键时,下拉列表仅在select2导轨中显示删除的项目

问题描述 投票:0回答:1

我在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个回答
0
投票

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

$.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();

并且可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.