简体   繁体   English

select2 - 为输入和下拉设置不同的宽度

[英]select2 - Setting different width to input and dropdown

I am using Select2 3.3.2我正在使用 Select2 3.3.2

I have very very long options in the select.我在选择中有很长的选项。 Example:例子:

<select id="e1">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
<option value="WY">very long long long text</option>
<option value="WY">very long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long long text</option>
</select>

The purpose is to avoid wrapping of the options when the dropdown opens up.目的是避免在打开下拉菜单时包装选项。

I would like to set long width for the dropdown when it opens up (for example - 800px, or even automatic evaluated width).我想在下拉菜单打开时为其设置长宽度(例如 - 800px,甚至自动评估宽度)。

But to keep the input with short width when the dropdown is closed (for example 300px).但是要在关闭下拉菜单时保持输入宽度较短(例如 300 像素)。

I followed this "Add option to set dropdown width" issue of select2.我遵循了 select2 的“添加选项以设置下拉宽度”问题。

But I couldn't get it to work, the width of the options/dropdown is the same as the input's width and the options as wrapped to multiple lines.但我无法让它工作,选项/下拉列表的宽度与输入的宽度相同,并且选项被包裹到多行。

Here is the demo of my problem in jsfiddle - http://jsfiddle.net/ewwAX/这是我在 jsfiddle 中的问题的演示 - http://jsfiddle.net/ewwAX/

Thank you all in advance for helping.提前感谢大家的帮助。

Select2 includes 'dropdownAutoWidth' parameter which uses javascript to attempt to be wide enough for the dropdown contents. Select2 包含“dropdownAutoWidth”参数,该参数使用 javascript 来尝试使下拉内容足够宽。

$('#whatever').select2({dropdownAutoWidth : true});

This at least works with Select2 3.4.3 - I don't know how much earlier it was introduced.这至少适用于 Select2 3.4.3 - 我不知道它被引入了多早。

The property you used is not for controlling the dropdown width.您使用的属性不是用于控制下拉宽度。 You can use the dropdownCssClass property.您可以使用dropdownCssClass属性。

Here's a demo in jsFiddle .这是 jsFiddle 中的演示

JavaScript : JavaScript

$(document).ready(function() { 
    $("#e1").select2({dropdownCssClass : 'bigdrop'}); 
});

CSS : CSS

.bigdrop {
    width: 600px !important;
}

Please note that for more recent versions there is now a better solution!请注意,对于更新的版本,现在有更好的解决方案! See Dave Amphlett's answer戴夫·安弗莱特的回答

$("#e1").select2({ width: '100%' });      

Try this.尝试这个。 This will set the width of container to 100%这会将容器的宽度设置为 100%

Even better solution.更好的解决方案。 Use:利用:

$('#e1').select2({width: 'resolve'});

尝试这个。

$(".js-example-basic-single").select2({dropdownAutoWidth : true});

尝试这个。

<select id="e1" data-width="100%">

Here's how to make select2 wider only when its open:以下是仅在 select2 打开时使其更宽的方法:

If you want it to expand towards the right side, add this css:如果您希望它向右侧扩展,请添加以下 css:

  .select2-container.select2-dropdown-open {
    width: 170%;
  }

if you want it to expand left, add this {如果你想让它向左扩展,添加这个 {

  .select2-container.select2-dropdown-open {
    width: 170%;
    margin-left: -70%;
  }

I used this and it worked perfect, only that it will affect all the select2我使用了它并且效果很好,只是它会影响所有select2

#select2-drop{
    width: 400px !important;
}

This worked for me..这对我有用..

 .select2-container--krajee .select2-dropdown--below{
    width:230px !important;
    margin-left: -70px !important;
}

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

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