【发布时间】:2019-03-12 05:20:31
【问题描述】:
我有一个包含六列的表,其中五列可以根据用户选择隐藏。我遇到的问题是,当隐藏列时,所有其他列都会扩展以填充隐藏列占用的空间。相反,我希望该列隐藏,其余列向左移动,同时保留给定的宽度。我在这里找到的答案似乎都没有解决这个特定问题。
下面是我的 js 和 css 的截图以及屏幕截图。
js:
<table className="hours table table-bordered table-sm" table-layout="fixed">
<thead>
<tr>
<th scope="col"
colSpan="3"
className="row-dow"
>
Hours
</th>
<th
scope="col"
colSpan="2"
align="center"
hidden={this.state.kitchenHoursSame}
className="row-16"
>
Kitchen Hours
</th>
<th
scope="col"
colSpan="2"
align="center"
hidden={!this.state.breakfast}
className="row-16"
>
Breakfast Special
</th>
...
</tr>
<tr>
<th
scope="col"
// className="row-8 "
>
Day
</th>
<th
scope="col"
className="row-8"
>
Open
</th>
<th
scope="col"
className="row-8"
>
Close
</th>
<th
scope="col"
hidden={this.state.kitchenHoursSame}
className="row-8"
>
Open
</th>
<th
scope="col"
hidden={this.state.kitchenHoursSame}
className="row-8"
>
Close
</th>
<th
scope="col"
hidden={!this.state.breakfast}
className="row-8"
>
1234567890123
</th>
<th
scope="col"
hidden={!this.state.breakfast}
className="row-8"
>
End
</th>
...
</tr>
</thead>
<tbody>
{this.state.DOW.map((dowList, i) =>
<tr>
<th scope="row" key={dowList.i}>{dowList.long}</th>
<td>
<select name="timeofday"
value={this.state.timeofday}
onChange={this.handleInputChange}>
<option>
(open)
</option>
</select>
</td>
...
</tr>
)}
</tbody>
</table>
css:
.hours {
table-layout: fixed;
/* width: 90%; */
width: 1500px;
white-space: nowrap;
}
.hours td {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.row-dow {
width: 20px;
}
.row-16 {
width: 16px;
}
.row-8 {
width: 8px;
}
即使有人告诉我不应该使用表格而应该使用其他东西,任何帮助都将不胜感激。
【问题讨论】:
-
表格宽度是 100% 吗?
-
没有。当前为 1500 像素。 .hours { 表格布局:固定; /* 宽度:90%; */ 宽度:1500px;空白:nowrap; }
-
@IvanBurnaev 说如果你给表格一个宽度,当列被删除时它会保留宽度。
-
您的表格应该有一个
auto宽度,以便在删除列时缩小。否则,如果表格保持它的大小,内部列将包含所有位置并且宽度会增加。
标签: css reactjs html-table css-tables