简体   繁体   English

如何设置 select2 下拉菜单的最小宽度和最大宽度?

[英]How to set min-width and max-width of select2 dropdown?

I have a select2 dropdown in a responsive div.我在响应式 div 中有一个 select2 下拉菜单。 This div also has a sidebar element/column that contains the user's selections from the dropdown.这个 div 还有一个侧边栏元素/列,其中包含用户从下拉列表中的选择。 They can select an option and it is added to the sidebar.他们可以选择 select 并将其添加到侧边栏。

在此处输入图像描述

Everything works fine, but I have one option in the dropdown that is very long.一切正常,但我在下拉列表中有一个很长的选项。 If the user select this, the width of the parent div is expanded and pushes the sidebar down below the div.如果用户 select 这个,则父 div 的宽度被扩展并将侧边栏向下推到 div 下方。

在此处输入图像描述

I think this is happening because the option is exceeding some predetermined width by select2.我认为发生这种情况是因为该选项超出了 select2 的某个预定宽度。 Setting dropdownAutoWidth to false doesn't fix the issue, so I thought of adding a dropdownCssClass with min-width and max-width set.dropdownAutoWidth设置为false并不能解决问题,因此我想添加一个设置了min-widthmax-widthdropdownCssClass

//in script.ts
$("#calltype").select2({ dropdownCssClass: 'smalldrop', width: 'auto' });
. . .
//calltype.scss
.smalldrop {
    min-width: 113px !important;
    max-width: 236px !important;
}

This will fix the issue, but now the dropdown isn't changing it's width to match the new width of the page if the user changes the screen size.这将解决问题,但如果用户更改屏幕尺寸,现在下拉菜单不会更改其宽度以匹配页面的新宽度。

App in small window小应用程序 window

在此处输入图像描述

App in maximized window应用程序在最大化 window

在此处输入图像描述

What can I do?我能做些什么? How can I set the min and max width of the dropdown and still retain the responsiveness of the normal select2 dropdown?如何设置下拉菜单的最小和最大宽度并仍然保留正常 select2 下拉菜单的响应能力?

EDIT: Requested sidebar code:编辑:请求的侧边栏代码:

<div id="listColumn" class="col-3">
    <div class="incident-container">
        <div id="incidentsListRow" class="row d-none mb-5">
            <div class="col">
                <h6>Incidents:</h6>
                <hr />
                <div class="row no-gutters">
                    <div class="col" id="incidentsList">
                </div>
            </div>
        </div>
    </div>
</div>

From the documentation , you can apply a class to the "original" <select> element and pass width: "element" as a Select2 parameter to have it to Uses the computed element width from any applicable CSS rules.文档中,您可以将 class 应用于“原始” <select>元素并将width: "element"作为 Select2 参数传递,以使其使用任何适用的 CSS 规则中计算出的元素宽度。

Below, you can see that the max and min width are applied.下面,您可以看到应用了最大和最小宽度。

 $("#calltype").select2({ width: 'element' }); $("#calltype2").select2({ width: 'element' }); // Just for this demo... To console log the width. $(".select2-container").on("mouseover", function(){ console.log( $(this).width() ) })
 .smalldrop { min-width: 113px;important: max-width; 236px !important; }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js"></script> <select id="calltype" class="smalldrop"> <option>min</option> <option>aaa</option> <option>aaa</option> </select> <br> <select id="calltype2" class="smalldrop"> <option>A very long option that would be more than the max-with normally...</option> <option>aaa</option> <option>aaa</option> </select>

After much tribulation, I solved the problem by placing min and max-width constraints on the parent div and just reverted to the normal seelct2 init.经过多次磨难,我通过在父 div 上放置最小和最大宽度约束来解决问题,然后恢复到正常的 seelct2 init。

//in script.ts
$("#calltype").select2({ dropdownAutoWidth: true, width: 'auto' });
. . .
//calltype.scss
.parent-div-container {
    max-width: 543px !important;
    min-width: 157px !important;
}

in my case i needed the parent-div to stay at 50px but the select2 drop-down to get 100px在我的情况下,我需要 parent-div 保持在 50px,但 select2 下拉菜单需要 100px

here is what worked for me without importing other libraries (it can be improved )这是在不导入其他库的情况下对我有用的方法(可以改进)

    $(function () {
        $(document).ready(function () {
            ...
            $(".select2-selection").on('click', function() {
                $('.select2-dropdown').css('min-width', '100px');
            });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM