Bootstrap 滚动表

我想在我的网站上有一个表。问题是这个表大约有400行。如何限制表的高度,并对其应用滚动条? 这是我的暗号:

<div class="span3">
<h2>Achievements left</h2>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Something</td>
</tr>
<tr>
<td>2</td>
<td>Something</td>
</tr>
<tr>
<td>3</td>
<td>Something</td>
</tr>
<tr>
<td>4</td>
<td>Something</td>
</tr>
<tr>
<td>5</td>
<td>Something</td>
</tr>
<tr>
<td>6</td>
<td>Something</td>
</tr>
</tbody>
</table>


<p><a class="btn" href="#">View details &raquo;</a></p>
</div>

我尝试应用最大高度和固定高度的表,但它不工作。

389311 次浏览

表元素似乎并不直接支持这一点。将表放置在 div 中,设置 div 的高度并设置 overflow: auto

.span3 {
height: 100px !important;
overflow: scroll;
}​

您可能希望将它包装在自己的 div 中,或者为 span 3赋予自己的 id,这样就不会影响整个布局。

这是小提琴: http://jsfiddle.net/zm6rf/

CSS

.achievements-wrapper { height: 300px; overflow: auto; }

超文本标示语言

<div class="span3 achievements-wrapper">
<h2>Achievements left</h2>
<table class="table table-striped">
...
</table>
</div>

我最近也遇到过类似的问题,最后用不同的解决方案混合在一起解决了。

第一个也是最简单的方法是使用两个表,一个用于表头,一个用于表体。这样可以工作,但是标题栏和主体栏没有对齐。而且,由于我想使用 Bootstrap 表的自动调整大小功能,我最终创建了一个 Javascript 函数,它可以在下列情况下更改标题: 正文被渲染、窗口被调整大小、列中的数据被更改等等。

下面是我使用的一些代码:

        <table class="table table-striped table-hover" style="margin-bottom: 0px;">
<thead>
<tr>
<th data-sort="id">Header 1</i></th>
<th data-sort="guide">Header 2</th>
<th data-sort="origin">Header 3</th>
<th data-sort="supplier">Header 4</th>
</tr>
</thead>
</table>
<div class="bodycontainer scrollable">
<table class="table table-hover table-striped table-scrollable">
<tbody id="rows"></tbody>
</table>
</div>

标题和主体分为两个独立的表。其中之一是在 DIV 中,该 DIV 具有生成垂直滚动条所需的样式。下面是我使用的 CSS:

.bodycontainer {
//height: 200px
width: 100%;
margin: 0;
}


.table-scrollable {
margin: 0px;
padding: 0px;
}

我在这里注释了高度,因为我希望表能够到达页面的底部,不管页面高度是多少。

我在头中使用的数据排序属性也在每个 td 中使用。这样我就可以得到每个 td 的宽度和填充以及行的宽度。使用我使用 CSS 设置的 data-sort 属性,相应地为每个标题和标题行设置填充和宽度,标题行总是更大,因为它没有滚动条。下面是使用 Coffee escript 的函数:

fixHeaders: =>
for header, i in @headers
tdpadding = parseInt(@$("td[data-sort=#{header}]").css('padding'))
tdwidth = parseInt(@$("td[data-sort=#{header}]").css('width'))
@$("th[data-sort=#{header}]").css('padding', tdpadding)
@$("th[data-sort=#{header}]").css('width', tdwidth)
if (i+1) == @headers.length
trwidth = @$("td[data-sort=#{header}]").parent().css('width')
@$("th[data-sort=#{header}]").parent().parent().parent().css('width', trwidth)
@$('.bodycontainer').css('height', window.innerHeight - ($('html').outerHeight() -@$('.bodycontainer').outerHeight() ) ) unless @collection.length == 0

在这里,我假设您有一个名为@headers 的头数组。

它不漂亮,但它的工作。希望它能帮助别人。

我有同样的问题,并使用了上述解决方案的组合(并增加了我自己的扭曲)。注意,我必须指定列宽,以保持头部和正文之间的一致性。

在我的解决方案中,当主体滚动时,页眉和页脚保持固定。

<div class="table-responsive">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th width="25%">First Name</th>
<th width="13%">Last Name</th>
<th width="25%" class="text-center">Address</th>
<th width="25%" class="text-center">City</th>
<th width="4%" class="text-center">State</th>
<th width="8%" class="text-center">Zip</th>
</tr>
</thead>
</table>
<div class="bodycontainer scrollable">
<table class="table table-hover table-striped table-condensed table-scrollable">
<tbody>
<!-- add rows here, specifying same widths as in header, at least on one row -->
</tbody>
</table>
</div>
<table class="table table-hover table-striped table-condensed">
<tfoot>
<!-- add your footer here... -->
</tfoot>
</table>
</div>

然后应用下面的 CSS:

.bodycontainer { max-height: 450px; width: 100%; margin: 0; overflow-y: auto; }
.table-scrollable { margin: 0; padding: 0; }

我希望这能帮到别人。

所有上述解决方案使表头滚动太..。 如果你只想滚动一个主体,那么应用这个:

tbody {
height: 100px !important;
overflow: scroll;
display:block;
}

不需要把它包起来。

CSS :

tr {
width: 100%;
display: inline-table;
table-layout: fixed;
}


table{
height:300px;              // <-- Select the height of the table
display: -moz-groupbox;    // Firefox Bad Effect
}
tbody{
overflow-y: scroll;
height: 200px;            //  <-- Select the height of the body
width: 100%;
position: absolute;
}

Bootply.com/AgI8LpDugl”rel = “ noReferrer”> http://www.Bootply.com/agi8lpdugl

我最近不得不用 bootstrap 和 angularjs 来做这件事,我创建了一个 angularjs 指令来解决这个问题,你可以在这个 博客文章上看到一个工作示例和详细信息。

只需在表格标签中添加一个固定标题属性即可:

<table class="table table-bordered" fixed-header>
...
</table>

如果不使用 angularjs,可以调整指令的 javascript,直接使用它。

这可能不是大行计数的解决方案。我只是使用复制表的技巧来做这件事,为小行数表,同时它可以保持灵活的列宽度,你可以尝试这个 小提琴

(固定宽度列是我用于大型行计数表的方法,它只需要标题行复制)

示例代码:

<div style="height:30px;overflow:hidden;margin-right:15px;">
<table class="table">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cel 1,1</td>
<td>Cel 1,2</td>
<td>Cel 1,3</td>
</tr>
<tr>
<td>Cel 2,1</td>
<td>Cel 2,2</td>
<td>Cel 2,3</td>
</tr>
<tr>
<td>Cel 3,1</td>
<td>Cel 3,2</td>
<td>Cel 3,3</td>
</tr>




</tbody>
</table>
</div>
<div style="height:100px;overflow-y:scroll;;">
<table class="table">
<thead>


</thead>
<tbody>
<tr>
<td>Cel 1,1</td>
<td>Cel 1,2</td>
<td>Cel 1,3</td>
</tr>
<tr>
<td>Cel 2,1</td>
<td>Cel 2,2</td>
<td>Cel 2,3</td>
</tr>
<tr>
<td>Cel 3,1</td>
<td>Cel 3,2</td>
<td>Cel 3,3</td>
</tr>
<tr style="color:white">
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</tbody>
</table>
</div>

这些答案对我来说都不令人满意。它们要么没有在适当的位置修复表格标题行,要么需要固定的列宽才能正常工作,即使这样,在不同的浏览器中,标题行和主体行也往往不能对齐。

我建议咬紧牙关,使用适当的网格控制,如 JsGrid或我个人的最爱,SlickGrid。它显然引入了一个依赖项,但是如果你想让你的表表现得像真正的网格一样,支持跨浏览器,这将会省去你的麻烦。它还提供了排序和调整列大小的选项,如果需要,还可以提供大量其他特性。

重申乔纳森 · 伍德的建议。我没有选择用一个新的 div 包装桌子,因为我正在使用 CMS。使用 JQuery 我是这样做的:

$( "table" ).wrap( "<div class='table-overflow'></div>" );

这将使用类“ table-overflow”的新 div 来包装表元素。

然后,您可以简单地在 css 文件中添加以下定义:

.table-overflow { overflow: auto; }

将表放在一个 div 中,并将类 pre-scrollable赋给该 div。

我想我会张贴在这里,因为我不能得到任何以上与 Bootstrap 4工作。我在网上找到了这个,非常适合我。

table
{
width: 100%;
display: block;
border:solid black 1px;
}


thead
{
display: inline-block;
width: 100%;
height: 20px;
}


tbody
{
height: 200px;
display: inline-block;
width: 100%;
overflow: auto;
}


th, td
{
width: 100px;
border:solid 1px black;
text-align:center;
}

我还附加了工作 jsfindle。

Https://jsfiddle.net/jgngg28t/

希望能帮到别人。

把表格放在 div 里面,使得表格可以垂直滚动。将 overflow-y改为 overflow-x,使表可水平滚动。只需要 overflow就可以使表格在水平和垂直方向都可以滚动。

<div style="overflow-y: scroll;">
<table>
...
</table>
</div>

这个例子展示了如何在使用 Bootstrap 4表样式时使用粘贴头。

.table-scrollable {
/* set the height to enable overflow of the table */
max-height: 200px;
    

overflow-x: auto;
overflow-y: auto;
scrollbar-width: thin;
}


.table-scrollable thead th {
border: none;
}


.table-scrollable thead th {
/* Set header to stick to the top of the container. */
position: sticky;
top: 0px;
    

/* This is needed otherwise the sticky header will be transparent
*/
background-color: white;


/* Because bootstrap adds `border-collapse: collapse` to the
* table, the header boarders aren't sticky.
* So, we need to make some adjustments to cover up the actual
* header borders and created fake borders instead
*/
margin-top: -1px;
margin-bottom: -1px;


/* This is our fake border (see above comment) */
box-shadow: inset 0 1px 0 #dee2e6,
inset 0 -1px 0 #dee2e6;
}
<!-- CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">


<!-- jQuery and JS bundle w/ Popper.js -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>




<div class="dashboard-container card">
<div class="card-body">
<div class="table-scrollable">
<table class="table table-hover table-sortable">
<thead>
<tr>
<th data-sort-type="text">Course</th>
<th data-sort-type="numeric">In Progress</th>
<th data-sort-type="numeric">Not Started</th>
<th data-sort-type="numeric">Passed</th>
<th data-sort-type="numeric">Failed</th>
</tr>
</thead>
<tbody>
<tr>
<td>How to be good at stuff</td>
<td>0</td>
<td>1000</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>Quantum physics for artists</td>
<td>200</td>
<td>6</td>
<td>66</td>
<td>66</td>
</tr>
<tr>
<td>The best way to skin a cat</td>
<td>34</td>
<td>16</td>
<td>200</td>
<td>7</td>
</tr>
<tr>
<td>Human cookbook</td>
<td>4</td>
<td>7</td>
<td>4</td>
<td>50</td>
</tr>
<tr>
<td>Aristocracy rules</td>
<td>100</td>
<td>3</td>
<td>6</td>
<td>18</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

我把 .table-responsive加到桌子上,它起作用了。从 医生开始

通过包装任意。与。表响应{-sm |-md |-lg |-xl } ,使表在每个最大宽度断点水平滚动,分别达到(但不包括)576px、768px、992px 和1120px。