【发布时间】:2018-11-15 06:25:51
【问题描述】:
我在一个项目中使用带有 Angular 6 的引导表,并且我能够使用以下代码创建垂直滚动表体:
<table class="table table-striped table-bordered" style="margin-bottom: 0">
<thead> <!-- Column names -->
<tr>
<th scope="col">#</th>
<th scope="col">Col 1</th>
<th scope="col">Col 2</th>
<th scope="col">Col 3</th>
...
<th scope="col">Col N</th>
</tr>
</thead>
<tbody> <!-- Data -->
<tr>
<th scope="row">1</th>
<td>AAAAA</td>
<td>BBBBB</td>
<td>CCCCC</td>
...
<td>DDDDD</td>
</tr>
<tr>
<th scope="row">2</th>
<td>AAAAA</td>
<td>BBBBB</td>
<td>CCCCC</td>
...
<td>DDDDD</td>
</tr>
...
<tr>
<th scope="row">n</th>
<td>AAAAA</td>
<td>BBBBB</td>
<td>CCCCC</td>
...
<td>DDDDD</td>
</tr>
</tbody>
</table>
还有css:
tbody {
display:block;
max-height:500px;
overflow-y:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;
}
thead {
width: calc( 100% - 1em )
}
但是现在,如果有很多列就不好看了。
所以我也想添加一个水平滚动,这样我就可以水平滚动整个表格,而只垂直滚动表格主体。 要进行水平滚动,我使用了 .table-responsive ,如下所示:
<div class="table-responsive">
<table class="table table-striped table-bordered" style="margin-bottom: 0">
...
</table>
</div>
但它只适用于没有 css 中的垂直滚动部分。
我想结合这两种方式来滚动表格。 我将 css 部分的宽度值从 100% 更改为静态 px 值,如下所示:
...
thead, tbody tr {
display:table;
width: 2000px;
table-layout:fixed;
}
thead {
width: calc( 2000px - 1em )
}
它有效,但我需要设置一个静态宽度,我不知道如何动态地做到这一点(取决于列数)。
【问题讨论】:
标签: html css angular bootstrap-4