简体   繁体   English

使select2框足够容纳内容

[英]Make select2 boxes wide enough for contents

I have this table in jsfiddle . 我在jsfiddle中有这张桌子 I want the name column to be wide enough to fit the names available in the options without truncation. 我希望name列足够宽以适合options可用的名称而不会被截断。

I have explored the suggestions in the Select2 appearance documentation , but those don't seem to provide what I'm seeking. 我已经探究了Select2外观文档中的建议,但这些建议似乎没有提供我所需要的。

If possible, I would like it to automatically shrink or widen to fit the longest available text in the data . 如果可能的话,我希望它自动缩小或扩大以适合data最长的可用文本。

在此处输入图片说明

<table>
<tr><td>name</td><td>sex</td><td>age</td></tr>
<tr><td><select class="name" multiple="multiple"><option></option></select><td><input name="sex"/></td><td><input name=
"age"/></td></tr>
<tr><td><select class="name"/><td><input name="sex"/></td><td><input name=
"age"/></td></tr>
</table>

<script>
var options = [
  {id: 1, text: 'Alexander Allen Abrahms III'},
  {id: 2, text: 'Benjamin Barry Bautista Jr.'}
]



$(".name").select2({data:options, placeholder: 'Full name'});
</script>

You can use css and define style on select tag like this: 您可以使用css并在select标签上定义样式,如下所示:

<select class="name" style="width:100%" multiple="multiple">
   <option></option>
</select>

and define table cell width: 并定义表格单元格宽度:

<table>
   <thead>
     <tr>
       <th width=50%> Name </th>
       <th width=25%> Sex </th>
       <th width=20%> Age </th> 
    </tr>
   </thead>
   <tbody> Your table body</tbody>
</table>

Here is yours edited code in fiddle https://jsfiddle.net/3hrdrjj4/1/ 这是您在小提琴中编辑的代码https://jsfiddle.net/3hrdrjj4/1/

EDIT: 编辑:

Fiddle with select width responsive to picked options and fixed placeholder https://jsfiddle.net/3hrdrjj4/5/ 带有选择宽度的小提琴,可响应所选择的选项并固定占位符https://jsfiddle.net/3hrdrjj4/5/

if i didn't get it wrong u want this right? 如果我没有理解错ü想对吗?

you can just simply add style="width: 250px;" 您只需添加style="width: 250px;" to the inputs you desire. 到您想要的输入。 or add width: 250px; 或添加width: 250px; to the name class. name类。

https://jsfiddle.net/wLzxo9ba/5/ https://jsfiddle.net/wLzxo9ba/5/

The other way to resolve it is: 解决该问题的另一种方法是:

<table style="width:100%">
    <tr>
        <td style="width:50%">name</td>
        <td style="width:15%">sex</td>
        <td style="width:15%">age</td>
    </tr>
    <tr>
        <td style="width:50%"><select class="name" multiple="multiple" style="width:100%"><option></option></select>
        <td style="width:15%"><input name="sex"/></td>
        <td style="width:15%"><input name="age"/></td>
    </tr>
    <tr>
        <td style="width:50%"><select class="name"  style="width:100%"/>
        <td style="width:15%"><input name="sex"/></td>
        <td style="width:15%"><input name="age"/></td>
    </tr>
</table>

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

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