【问题标题】:set the width of select2 input (through Angular-ui directive)设置 select2 输入的宽度(通过 Angular-ui 指令)
【发布时间】:2012-09-22 22:18:54
【问题描述】:

我无法让这个 plunkr (select2 + angulat-ui) 工作。

http://plnkr.co/edit/NkdWUO?p=preview

在本地设置中,我得到了 select2 的工作,但我无法按照docs 中的描述设置宽度。它太窄而无法使用。

谢谢。

编辑: 没关系那个plnkr,我在这里找到了一个工作小提琴 http://jsfiddle.net/pEFy6/

看起来这是 select2 折叠到第一个元素的宽度的行为。我可以通过 bootstrap class="input-medium" 设置宽度。仍然不确定为什么 angular-ui 不接受配置参数。

【问题讨论】:

  • 您能更具体地说明您已经尝试过的内容吗?所选库(select2 所基于)将根据 HTML/CSS 中提供的宽度生成宽度。

标签: css twitter-bootstrap angularjs jquery-select2


【解决方案1】:

在我的情况下,如果有零个或多个药丸,select2 会正确打开。

但如果有一颗或多颗药丸,我将它们全部删除,它会缩小到最小的宽度。我的解决方案很简单:

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

【讨论】:

  • 如果使用 select2 版本 4.0.0 的最佳答案
  • 为什么这比 $("#myselect").select2({ width: 'resolve' }); ? (好像是一样的)
  • @RicardoVigatti: width:'resolve' 仅在页面加载时有效,但在调整大小时无效。如果您使用的是响应式设计,它就没有效果
  • resolve 对我不起作用 - 100%` 起作用了。那是半个小时绝望地寻找“已解决”的答案。谢谢:)
  • 这是迄今为止最好的答案。但是现在,对我来说,搜索字段并没有填满所有的宽度。有什么简单的方法可以纠正这个问题?
【解决方案2】:

您需要指定要解析的属性宽度以保留元素宽度

$(document).ready(function() { 
    $("#myselect").select2({ width: 'resolve' });           
});

【讨论】:

  • $("#myselect").select2({ placeholder: 'Select Country', width: '192px' });解决了我的 IE 问题,对 FF 和 Chrome 没有影响!谢谢!
  • 是的。我还问了作者github.com/godbasin/vue-select2/issues/11
【解决方案3】:

这是一个老问题,但新信息仍然值得发布...

从 Select2 版本 3.4.0 开始,有一个属性 dropdownAutoWidth 可以解决问题并处理所有奇怪的情况。请注意,默认情况下不启用。它会在用户进行选择时动态调整大小,如果使用该属性,它会为 allowClear 添加宽度,并且它也会正确处理占位符文本。

$("#some_select_id").select2({
    dropdownAutoWidth : true
});

【讨论】:

  • 它工作得很好......除非你使用占位符,如果你是,如果你的选项小于占位符,占位符文本将被截断:(
【解决方案4】:

select2 V4.0.3

<select class="smartsearch" name="search" id="search" style="width:100%;"></select>

【讨论】:

    【解决方案5】:

    我正在使用 > 4.0 的 select2

    $("select").select2({
      dropdownAutoWidth: true
    });
    

    这些其他的没有用:

    • dropdownCssClass: 'bigdrop'
    • 宽度:'100%'
    • 宽度:“解决”

    【讨论】:

      【解决方案6】:

      像这样在脚本中添加方法容器 css:

      $("#your_select_id").select2({
            containerCss : {"display":"block"}
      });
      

      它会将您的选择的宽度设置为与您的 div 的宽度相同。

      【讨论】:

      • 这对我有用(我无法让 select2 使用 Bootstrap 3 自动调整大小)。虽然我不得不说这有点hacky。
      • 这让我走上了正轨,我用 minWidth 替换了 display:$( '.opt-option-type-select' ).select2( { containerCss : { minWidth: '10em', }, } );
      【解决方案7】:

      为您的 select2 函数添加宽度解析选项

      $(document).ready(function() { 
              $("#myselect").select2({ width: 'resolve' });           
      });
      

      将下面的 CSS 添加到您的样式表后

      .select2-container {
      width: 100% !important;
      }
      

      它将对问题进行排序

      【讨论】:

        【解决方案8】:

        将宽度设置为“解决”对我不起作用。 相反,我在我的选择中添加了“width:100%”,并像这样修改了 css:

        .select2-offscreen {position: fixed !important;}

        这是唯一对我有用的解决方案(我的版本是 2.2.1) 您的选择将占据所有可用的位置,这比使用

        class="input-medium"

        来自引导程序,因为选择始终留在容器中,即使在调整窗口大小(响应式)之后也是如此

        【讨论】:

        • 感谢您的解决方案,但它仅在第一次加载时对我有用。一旦我选择了一个标签,然后单击另一个页面元素,select2 输入就会回到不正确的大小
        【解决方案9】:

        也想在这里添加我的解决方案。这类似于上面 Ravindu 的回答。

        我在 select2 中添加了width: 'resolve', 作为属性:

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

        然后我将min-width 属性添加到select2-container!important

        .select2-container {
            min-width: 100% !important;
        }
        

        在 select 元素上,style='100px !important;' 属性需要!important 才能工作:

        <select class="form-control" style="width: 160px;" id="searchFilter" name="searchfilter[]">
        

        【讨论】:

          【解决方案10】:

          在你的样式表中添加这个:

          .select2 {
            width: unset !important;
          }
          

          【讨论】:

            【解决方案11】:

            你可以这样试试。它对我有用

            $("#MISSION_ID").select2();

            在隐藏/显示或 ajax 请求时,我们必须重新初始化 select2 插件

            例如:

            $("#offialNumberArea").show();
            $("#eventNameArea").hide();
            
            $('.selectCriteria').change(function(){
                var thisVal = $(this).val();
                if(thisVal == 'sailor'){
                    $("#offialNumberArea").show();
                    $("#eventNameArea").hide();
                }
                else
                {
                    $("#offialNumberArea").hide();
                    $("#eventNameArea").show();
                    $("#MISSION_ID").select2();
                }
            });
            

            【讨论】:

              【解决方案12】:

              独立于 select2 的更简单的 CSS 解决方案

              //HTML
              <select id="" class="input-xlg">
              </select>
              <input type="text" id="" name="" value="" class="input-lg" />
              
              //CSS
              .input-xxsm {
                width: 40px!important; //for 2 digits 
              }
              
              .input-xsm {
                width: 60px!important; //for 4 digits 
              }
              
              .input-sm {
                width: 100px!important; //for short options   
              }
              
              .input-md {
                width: 160px!important; //for medium long options 
              }
              
              .input-lg {
                width: 220px!important; //for long options    
              }
              
              .input-xlg {
                width: 300px!important; //for very long options   
              }
              
              .input-xxlg {
                width: 100%!important; //100% of parent   
              }
              

              【讨论】:

                【解决方案13】:

                在最近使用 Bootstrap 4 构建的项目中,我尝试了上述所有方法,但没有任何效果。我的方法是使用 jQuery 编辑 CSS 以使 100% 出现在桌面上。

                 // * Select2 4.0.7
                
                $('.select2-multiple').select2({
                    // theme: 'bootstrap4', //Doesn't work
                    // width:'100%', //Doesn't work
                    width: 'resolve'
                });
                
                //The Fix
                $('.select2-w-100').parent().find('span')
                    .removeClass('select2-container')
                    .css("width", "100%")
                    .css("flex-grow", "1")
                    .css("box-sizing", "border-box")
                    .css("display", "inline-block")
                    .css("margin", "0")
                    .css("position", "relative")
                    .css("vertical-align", "middle")
                

                工作演示

                $('.select2-multiple').select2({
                        // theme: 'bootstrap4', //Doesn't work
                        // width:'100%',//Doens't work
                        width: 'resolve'
                    });
                    //Fix the above style width:100%
                    $('.select2-w-100').parent().find('span')
                        .removeClass('select2-container')
                        .css("width", "100%")
                        .css("flex-grow", "1")
                        .css("box-sizing", "border-box")
                        .css("display", "inline-block")
                        .css("margin", "0")
                        .css("position", "relative")
                        .css("vertical-align", "middle")
                <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
                <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.7/css/select2.min.css" rel="stylesheet" />
                
                <div class="table-responsive">
                    <table class="table">
                        <thead>
                        <tr>
                            <th scope="col" class="w-50">#</th>
                            <th scope="col" class="w-50">Trade Zones</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td>
                                1
                            </td>
                            <td>
                                <select class="form-control select2-multiple select2-w-100" name="sellingFees[]"
                                        multiple="multiple">
                                    <option value="1">One</option>
                                    <option value="1">Two</option>
                                    <option value="1">Three</option>
                                    <option value="1">Okay</option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                
                <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.7/js/select2.min.js"></script>

                【讨论】:

                  【解决方案14】:

                  您可以使用的其他方法是在 Select 标记上设置样式的宽度。

                  <select id="myselect" style="width: 20%;">
                      <option>...</option>
                  </select>
                  

                  然后,使用这个

                  $(document).ready(function() { 
                      $("#myselect").select2({ width: 'style' }); //This will use style attr of the select element  
                  });
                  

                  【讨论】:

                    猜你喜欢
                    • 2013-02-05
                    • 2013-05-07
                    • 1970-01-01
                    • 2019-05-06
                    • 1970-01-01
                    • 2016-02-22
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-08-26
                    相关资源
                    最近更新 更多