reactjs - bootstrap:表中的输入宽度对于所有列都是相同的,与列内容无关

时间:2017-11-21 11:09:06

标签: css reactjs bootstrap-4

我在reactjs中创建一个bootstrap表。该表在每个列标题下面都有一个过滤器。

以下是代码:

<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>

,输出为: enter image description here

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

enter image description here

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

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要在<input />固定宽度或填充中添加Css样式。 css box model

答案 1 :(得分:0)

您可以在名称列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

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

<th class="w-100">...</th>