具有模态滚动的Select2 Bootstrap模态

icnyk63a  于 2023-02-06  发布在  Bootstrap
关注(0)|答案(5)|浏览(166)
    • 更新日期:**

我补充道:

$("#id_project").select2({dropdownParent: $(".modal-body")});

...这会导致下拉菜单正确放置,并且可以键入搜索。不幸的是...这会导致传递的数据被清除,并且没有可供选择的选项。
我有一个在Bootstrap模态中填充的表单,如下所示:

<form method="post" action="/dataanalytics/sparc/tasks/create/" class="js-task-create-form">
  <input type="hidden" name="csrfmiddlewaretoken" value="W6cnRq9zPDvUdQOpqceXPVulbWJAMFazRStzwnZhLi8UEqa87SYiRKmMoHAKwmvb">
  <div class="modal-body">
  <div class="form-group">
    <select name="project" data-minimum-input-length="2" class="form-control select2-hidden-accessible" required="" id="id_project" data-autocomplete-light-url="/dataanalytics/projectautocomplete/" data-autocomplete-light-function="select2" tabindex="-1" aria-hidden="true">
       <option value="" selected="">---------</option>
    </select>
    <span class="select2 select2-container select2-container--bootstrap select2-container--focus" dir="ltr" style="width: 505.091px;">
    <span class="selection"><span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-labelledby="select2-id_project-container">
    <span class="select2-selection__rendered" id="select2-id_project-container">
    <span class="select2-selection__placeholder">
    </span>
    </span>
    <span class="select2-selection__arrow" role="presentation"><b role="presentation"></b></span></span></span><span class="dropdown-wrapper" aria-hidden="true">
    </span>
    </span>
  </div>
  <div class="form-group">
    <select name="dataSources" data-minimum-input-length="2" class="form-control select2-hidden-accessible" id="id_dataSources" data-autocomplete-light-url="/dataanalytics/datasourceautocomplete/" data-autocomplete-light-function="select2" multiple="" tabindex="-1" aria-hidden="true">
    </select>
    <span class="select2 select2-container select2-container--bootstrap" dir="ltr" style="width: 505.091px;">
    <span class="selection"><span class="select2-selection select2-selection--multiple" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="-1">
      <ul class="select2-selection__rendered">
         <li class="select2-search select2-search--inline">
         <input class="select2-search__field" type="search" tabindex="0" autocomplete="off" autocorrect="off" autocapitalize="none" spellcheck="false" role="textbox" aria-autocomplete="list" placeholder="" style="width: 0.75em;">
         </li>
      </ul>
    </span>
    </span>
    <span class="dropdown-wrapper" aria-hidden="true">
    </span>
    </span>
  </div>
</div>
</form>
</div>
</div>

带有select2单变量的第一个窗体组表现出有问题的行为。带有select2多变量的第二个窗体组按预期工作。为什么这里有差异?
JavaScript语言:

var loadForm = function () {
    var btn = $(this);
    $.ajax({
      url: btn.attr("data-url"),
      type: 'get',
      dataType: 'json',
      beforeSend: function () {
        $("#modal-task").modal("show");
      },
      success: function (data) {
        $("#modal-task .modal-content").html(data.html_form);
      }
    });
  };

版本:

  • jQuery语言版本:1.12.1
  • 选择2:4.0.5
  • Bootstrap :4

在Chrome中一切都很好。在Firefox中,模态中的Select2框不允许输入搜索输入。这是一个已知的问题(https://select2.org/troubleshooting/common-problems),但没有一个已知的解决方案似乎对我有效。
为了解决这个问题,我添加了以下代码来设置每个Select2的dropdownParent

$.fn.select2.defaults.set("dropdownParent", $('#modal-task'));

这允许键入搜索输入。但是,我的模态太长,需要滚动。如果模态滚动到原始位置之外,单击Select2框会导致选项和搜索输入出现在模态顶部的框上方。这是有意义的,因为所有Select2框都使用dropdownParent绑定到模态本身。
如何防止此行为并强制Select2选项和搜索输入正常显示(直接位于Select2的上方或下方,具体取决于它相对于窗口边缘的位置)?此外,即使未激活模态,页面上也有Select2框,因此以这种方式影响所有Select2框并不理想。

    • 更新:**我尝试用其他代码替换此行:
$.fn.modal.Constructor.prototype.enforceFocus = function () {};

这与什么都不做具有相同的效果:搜索输入在Firefox中不起作用,但下拉列表的位置正确。

    • 更新2:**我尝试了以下操作:
$('#id_project').set("dropdownParent", $('#id_project'));

这导致下拉菜单不出现在任何地方,选项也消失了(被---------取代)。

    • 更新3:**我尝试了以下操作...
$('#modal-task').css('overflow','visible');

...这导致搜索输入正常工作...但模式滚动现在已中断。此外,Select2框左边缘的下拉菜单略微未对齐。

vybvopom

vybvopom1#

这对我来说很好用

$(document).ready(function () {
    $('select.select2:not(.normal)').each(function () {
        $(this).select2({
            dropdownParent: $(this).parent().parent()
        });
    });
});
x8diyxa7

x8diyxa72#

只需像这样使用select2来绕过引导模式错误:

$('.my-select2').each(function () {
    $(this).select2({
        dropdownParent: $(this).parent(),// fix select2 search input focus bug
    })
})

// fix select2 bootstrap modal scroll bug
$(document).on('select2:close', '.my-select2', function (e) {
    var evt = "scroll.select2"
    $(e.target).parents().off(evt)
    $(window).off(evt)
})
f5emj3cl

f5emj3cl3#

我快迟到了,可以帮助别人。如果你的模态表中有tabindex = -1,那么你可能无法输入。试着删除它。对我很有效。

uxh89sit

uxh89sit4#

对我来说,我使用的是select2v4和bootstrap模式,而change事件是在老版本的select2上运行的。
我需要更新我的事件侦听器

selectList.on('change', function(e) {

选择(添加项目)

selectList.on('select2:select', function(e) {

和取消选择(删除项目)

selectList.on('select2:unselect', function(e) {

https://select2.org/programmatic-control/events

gt0wga4j

gt0wga4j5#

首先在select2选项中必须有

dropdownParent: $(this).parent(),// That fix weird positioning select2 input

之后我们在select2打开事件时做了一些小技巧。我意识到如果模态高度小于窗口高度,当select2打开时滚动没有问题,即使在需要滚动之后。
所以我们对select2事件进行了溢出调整。当打开select2时,将模态体设置为无溢出,在select2打开后,将其设置回默认值。

$(document).on("select2:opening", (e) => {
        $('.modal-body').css('overflow', 'visible');
    });
    $(document).on("select2:open", (e) => {
        $('.modal-body').css('overflow', 'auto');
    });

这会解决你的问题。

相关问题