【问题标题】:reactjs - bootstrap: input width within table same for all columns irrespective of column contentsreactjs - bootstrap:表内的输入宽度对于所有列都相同,而与列内容无关
【发布时间】:2018-05-04 19:55:06
【问题描述】:

我正在 reactjs 中创建一个引导表。该表在每个列标题下方都有一个过滤器。

以下是代码:

<table className="table table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th onClick={() => this.handleColumnSort("id")}><b>Id</b> <i className={`fa fa-fw ${this.handleColumnSortCss("id")}`}></i></th>
      <th onClick={() => this.handleColumnSort("name")}><b>Name</b> <i className={`fa fa-fw ${this.handleColumnSortCss("name")}`}></i></th>
      <th onClick={() => this.handleColumnSort("munit")}><b>Munit</b> <i className={`fa fa-fw ${this.handleColumnSortCss("munit")}`}></i></th>
      <th onClick={() => this.handleColumnSort("rate")}><b>Rate</b> <i className={`fa fa-fw ${this.handleColumnSortCss("rate")}`}></i></th>
    </tr>
    <tr>
      <th></th>
      <th><input  type="text" onChange={(e) => this.onChangeHandler("id",e)}/></th>
      <th><input  type="text" onChange={(e) => this.onChangeHandler("name",e)}/></th>
      <th><input  type="text" onChange={(e) => this.onChangeHandler("munit",e)}/></th>
      <th><input  type="text" onChange={(e) => this.onChangeHandler("rate",e)}/></th>
    </tr>
  </thead>
  <tbody>
  {this.props.items.filtereditems.map((item,index) => (
    <tr key={index} >
        <th> {(index+1)+((this.props.page_number-1)*(this.props.page_size))}</th>
        <td> {item.id}</td>
        <td> {item.name}</td>
        <td> {item.munit}</td>
        <td> {item.rate}</td>
    </tr>
    ))}
  </tbody>
</table>

输出是:

但是没有输入它看起来好多了,它会自动调整到内容宽度。

那么如何在输入宽度不扩展到最大的情况下实现上述布局。

【问题讨论】:

    标签: css reactjs bootstrap-4


    【解决方案1】:

    如果我理解正确,您只需将 Css 样式添加到您的 &lt;input /&gt; 固定宽度或填充。 css box model

    【讨论】:

      【解决方案2】:

      您可以通过将名称列设为width: 100% 来实现所需的结果。

      名称列可以更宽,这反过来又会使其他列更小,同时尊重流体宽度。

       <table>
        <thead>
          <tr>
            <th>#</th>
            <th>Id</th>
            <th>Name</th>
            <th>Munit</th>
            <th>Rate</th>
          </tr>
          <tr>
            <th></th>
            <th><input type="text" /></th>
            <th><input type="text" /></th>
            <th><input type="text" /></th>
            <th><input type="text" /></th>
          </tr>
        </thead>
        <tbody>
            <td>1</td>
            <td>100</td>
            <td>Name</td>
            <td>100000</td>
            <td>Rate</td>
        </tbody>
      </table>
      

      例如,您可以像这样定位名称列:

      table th:nth-child(3),
      table td:nth-child(3) {
          width: 100%;
      }
      

      这还假设以下规则对表格元素有效:

      table {
          table-layout: auto;
      }
      

      小提琴:https://jsfiddle.net/49fach5w/

      正如您提到的,您使用 boostrap,您可以使用以下帮助程序类:

      w-100

      您可以将此应用于包含“名称”数据的每个表格单元格,例如:

      &lt;th class="w-100"&gt;...&lt;/th&gt;

      【讨论】:

      • 不提 colspan 是不可能的。因为它在没有输入的情况下自动生成,所以当我们添加输入时会出现问题
      • table th:nth-child(3),table td:nth-child(3){width: 100%;} 这背后有什么魔力
      • 我不认为这是应用此规则的最佳方式,但没有看到您的 CSS,这只是针对表格中第三列的示例。 (developer.mozilla.org/en-US/docs/Web/CSS/%3Anth-child)
      • 我没有任何 css 文件。我只是在使用 bootstrapv4。我想知道输入不适合列的原因
      • HTML 表格也将尝试适应表格中的所有列,因此即使我们声称“名称”列应该是 100% 宽度,它实际上不能这样做 - 因为它需要适合表格边界内的其他列。所以这真正的作用是让第三列尽可能地填充宽度,然后自动将剩余空间分配给表中的其他列(为它们提供所需的最小宽度)。
      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2016-02-06
      • 2019-03-05
      • 1970-01-01
      相关资源
      最近更新 更多