【问题标题】:Bootstrap responsive table with form input min width具有表单输入最小宽度的引导响应表
【发布时间】:2020-05-04 02:21:45
【问题描述】:

我正在尝试创建一个响应式表格,其中包含多列的表单输入。据我了解,响应式表会缩小,直到其列不能再进一步,然后它会添加一个滚动条。但是,列缩小到我看不到输入字段中的值的程度,因此我需要一种方法来对这些列施加最小宽度,而不是让它由列标题文本长度确定。

我尝试将 min-width:"10%" 或 "150px" 添加到列中的所有元素,但没有奏效。这是我的设置:

<form class="form-horizontal">
    <div class="table-responsive">
        <table class="table table-bordered table-striped table-highlight">
            <thead>
                <th>Name</th>
                <th>Date</th>
                <th>Cost</th>
                <th>Billing Type</th>
                <th>Tax</th>
                <th>Other Col</th>
                <th>Other Col</th>
                <th>Other Col</th>
                <th>Total</th>
            </thead>
            <tbody>
                <tr>
                    <td><input type="text" class="form-control" value="Some Long Name Here"/></td>
                    <td><input type="text" class="form-control" value="13-Jul-2015"/></td>
                    <td><input type="text" class="form-control" value="$12355.12"/></td>
                    <td>
                        <select class="form-control">
                            <option>Monthly</option>
                            <option>Yearly</option>
                        </select>
                    </td>
                    <td><input type="text" class="form-control" value="another long value"/></td>
                    <td><input type="text" class="form-control" value="some val"/></td>
                    <td><input type="text" class="form-control" value="some val"/></td>
                    <td><input type="text" class="form-control" value="some val"/></td>
                    <td>$505.79</td>
                </tr>
            </tbody>
        </table>
    </div>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<form class="form-horizontal">
  <div class="table-responsive">
    <table class="table table-bordered table-striped table-highlight">
      <thead>
        <th>Name</th>
        <th>Date</th>
        <th>Cost</th>
        <th>Billing Type</th>
        <th>Tax</th>
        <th>Other Col</th>
        <th>Other Col</th>
        <th>Other Col</th>
        <th>Total</th>
      </thead>
      <tbody>
        <tr>
          <td><input type="text" class="form-control" value="Some Long Name Here" /></td>
          <td><input type="text" class="form-control" value="13-Jul-2015" /></td>
          <td><input type="text" class="form-control" value="$12355.12" /></td>
          <td>
            <select class="form-control">
                            <option>Monthly</option>
                            <option>Yearly</option>
                        </select>
          </td>
          <td><input type="text" class="form-control" value="another long value" /></td>
          <td><input type="text" class="form-control" value="some val" /></td>
          <td><input type="text" class="form-control" value="some val" /></td>
          <td><input type="text" class="form-control" value="some val" /></td>
          <td>$505.79</td>
        </tr>
      </tbody>
    </table>
  </div>
</form>

如您所见,当表格缩小时,某些字段值会被截断。任何帮助表示赞赏。

编辑:由于保留了一些遗留代码,我希望喜欢继续使用表格。但是,如果需要,我愿意接受任何解决方案。

【问题讨论】:

  • 如果您使用的是 DataTables,为什么要使用 Bootstrap?
  • 因为datatables是一个js插件,bootstrap是一个css/js框架。 DT 只需要我网站的一小部分组件。
  • form-control 强制这种行为,我相信,你的问题是 &lt;input&gt;

标签: html twitter-bootstrap twitter-bootstrap-3


【解决方案1】:

.form-control 的默认 css 会强制执行此行为,因此您需要更改输入字段的 css,例如:

input.form-control {
  width: auto;
}

【讨论】:

  • 这成功了,谢谢!简单的实现。我还能够修改它以使用特定宽度或使用 min-width 代替,等等...
  • 聪明。谢谢
【解决方案2】:

列宽将是动态的,因为用户可以输入任何长度值!将输入字段包裹在 div 中,使用 JavaScript 动态设置 div 宽度:

                <tr>
<td><div style="width:10em"><input onkeyup="updateWidth(this)" type="text" class="form-control" size="80" value="Some Long Name Here"/></div></td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)" type="text" class="form-control" value="13-Jul-2015"/></div></td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)" type="text" class="form-control" value="$12355.12"/></div></td>
<td>
    <select class="form-control">
        <option>Monthly</option>
        <option>Yearly</option>
    </select>
</td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)"  type="text" class="form-control" value="another long value"/></div></td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)"  type="text" class="form-control" value="some val"/></div></td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)"  type="text" class="form-control" value="some val"/></div></td>
<td><div style="width:10em"><input onkeyup="updateWidth(this)"  type="text" class="form-control" value="some val"/></div></td>
<td>$505.79</td>
            </tr>

还有JS函数:

 <script>
 function updateWidth(textbox) {
         $(textbox).parent().css("width",(textbox.value.length + 2) + "em");
 }
 </script>

【讨论】:

    【解决方案3】:

    要添加到@katwhocodes,如果您使用的是 Bootstrap 4, 您可以将w-auto 类添加到&lt;input&gt;

    https://getbootstrap.com/docs/4.4/utilities/sizing/#relative-to-the-parent

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 2014-04-09
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 2023-04-08
      • 2016-04-30
      • 1970-01-01
      相关资源
      最近更新 更多