Does anyone know how to make a table with a fixed header row and scrollable body rows? I'm using twitter bootstrap if that matters.
有谁知道如何制作一个固定标题行和可滚动的主体行的表?如果重要的话,我正在使用twitter bootstrap。
This is an example of what I'm trying to create:
这是我正在尝试创建的示例:
http://www.siteexperts.com/tips/html/ts04/page1.asp
http://www.siteexperts.com/tips/html/ts04/page1.asp
All the examples I've seen break it into two separate tables. Was wondering if anyone has a more elegant solution.
我见过的所有例子都把它分成两个单独的表。想知道是否有人有更优雅的解决方案。
Twitter bootstrap also automatically adjusts the column sizes based on content so that's another reason I'd like to keep it in one table
Twitter引导程序还会根据内容自动调整列大小,这是我希望将其保留在一个表中的另一个原因
4 个解决方案
#1
51
Just stack two bootstrap tables; one for columns, the other for content. No plugins, just pure bootstrap (and that ain't no bs, haha!)
只需堆叠两个引导表;一个用于列,另一个用于内容。没有插件,只是纯粹的bootstrap(这不是没有bs,哈哈!)
<table id="tableHeader" class="table" style="table-layout:fixed">
<thead>
<tr>
<th>Col1</th>
...
</tr>
</thead>
</table>
<div style="overflow-y:auto;">
<table id="tableData" class="table table-condensed" style="table-layout:fixed">
<tbody>
<tr>
<td>data</td>
...
</tr>
</tbody>
</table>
</div>
演示JSFiddle
The content table div needs overflow-y:auto
, for vertical scroll bars. Had to use table-layout:fixed
, otherwise, columns did not line up. Also, had to put the whole thing inside a bootstrap panel to eliminate space between the tables.
对于垂直滚动条,内容表div需要overflow-y:auto。不得不使用表格布局:固定,否则,列没有排队。此外,必须将整个内容放在引导板中以消除表之间的空间。
Have not tested with custom column widths, but provided you keep the widths consistent between the tables, it should work.
尚未使用自定义列宽进行测试,但如果保持表之间的宽度一致,则应该可以正常工作。
// ADD THIS JS FUNCTION TO MATCH UP COL WIDTHS
$(function () {
//copy width of header cells to match width of cells with data
//so they line up properly
var tdHeader = document.getElementById("tableHeader").rows[0].cells;
var tdData = document.getElementById("tableData").rows[0].cells;
for (var i = 0; i < tdData.length; i++)
tdHeader[i].style.width = tdData[i].offsetWidth + 'px';
});
#2
9
Here is a jQuery plugin that does exactly that: http://fixedheadertable.com/
这是一个jQuery插件,它正是这样做的:http://fixedheadertable.com/
Usage:
用法:
$('selector').fixedHeaderTable({ fixedColumn: 1 });
$('selector')。fixedHeaderTable({fixedColumn:1});
Set the fixedColumn
option if you want any number of columns to be also fixed for horizontal scrolling.
如果要为水平滚动修复任意数量的列,请设置fixedColumn选项。
EDIT: This example http://www.datatables.net/examples/basic_init/scroll_y.html is much better in my opinion, although with DataTables you'll need to get a better understanding of how it works in general.
编辑:这个例子http://www.datatables.net/examples/basic_init/scroll_y.html在我看来要好得多,尽管使用DataTables你需要更好地了解它的工作原理。
EDIT2: For Bootstrap to work with DataTables you need to follow the instructions here: http://datatables.net/blog/Twitter_Bootstrap_2 (I have tested this and it works)- For Bootstrap 3 there's a discussion here: http://datatables.net/forums/discussion/comment/53462 - (I haven't tested this)
EDIT2:要使Bootstrap与DataTables一起使用,你需要按照这里的说明操作:http://datatables.net/blog/Twitter_Bootstrap_2(我已经测试了这个并且它可以工作) - 对于Bootstrap 3,这里有一个讨论:http:// datatables .net / forums / discussion / comment / 53462 - (我还没有测试过这个)
#3
4
Interesting question, I tried doing this by just doing a fixed position row, but this way seems to be a much better one. Source at bottom.
有趣的问题,我尝试通过只做一个固定的位置行来做这个,但这种方式似乎是一个更好的。来源于底部。
css
CSS
thead { display:block; background: green; margin:0px; cell-spacing:0px; left:0px; }
tbody { display:block; overflow:auto; height:100px; }
th { height:50px; width:80px; }
td { height:50px; width:80px; background:blue; margin:0px; cell-spacing:0px;}
html
HTML
<table>
<thead>
<tr><th>hey</th><th>ho</th></tr>
</thead>
<tbody>
<tr><td>test</td><td>test</td></tr>
<tr><td>test</td><td>test</td></tr>
<tr><td>test</td><td>test</td></tr>
</tbody>
http://www.imaputz.com/cssStuff/bigFourVersion.html
http://www.imaputz.com/cssStuff/bigFourVersion.html
#4
0
<table class="table table-striped table-condensed table-hover rsk-tbl vScrollTHead">
<thead>
<tr>
<th>Risk Element</th>
<th>Description</th>
<th>Risk Value</th>
<th> </th>
</tr>
</thead>
</table>
<div class="vScrollTable">
<table class="table table-striped table-condensed table-hover rsk-tbl vScrollTBody">
<tbody>
<tr class="">
<td>JEWELLERY</td>
<td>Jewellery business</td>
</tr><tr class="">
<td>NGO</td>
<td>none-governmental organizations</td>
</tr>
</tbody>
</table>
</div>
.vScrollTBody{
height:15px;
}
.vScrollTHead {
height:15px;
}
.vScrollTable{
height:100px;
overflow-y:scroll;
}
having two tables for head and body worked for me
有两张头和身体的桌子为我工作
#1
51
Just stack two bootstrap tables; one for columns, the other for content. No plugins, just pure bootstrap (and that ain't no bs, haha!)
只需堆叠两个引导表;一个用于列,另一个用于内容。没有插件,只是纯粹的bootstrap(这不是没有bs,哈哈!)
<table id="tableHeader" class="table" style="table-layout:fixed">
<thead>
<tr>
<th>Col1</th>
...
</tr>
</thead>
</table>
<div style="overflow-y:auto;">
<table id="tableData" class="table table-condensed" style="table-layout:fixed">
<tbody>
<tr>
<td>data</td>
...
</tr>
</tbody>
</table>
</div>
演示JSFiddle
The content table div needs overflow-y:auto
, for vertical scroll bars. Had to use table-layout:fixed
, otherwise, columns did not line up. Also, had to put the whole thing inside a bootstrap panel to eliminate space between the tables.
对于垂直滚动条,内容表div需要overflow-y:auto。不得不使用表格布局:固定,否则,列没有排队。此外,必须将整个内容放在引导板中以消除表之间的空间。
Have not tested with custom column widths, but provided you keep the widths consistent between the tables, it should work.
尚未使用自定义列宽进行测试,但如果保持表之间的宽度一致,则应该可以正常工作。
// ADD THIS JS FUNCTION TO MATCH UP COL WIDTHS
$(function () {
//copy width of header cells to match width of cells with data
//so they line up properly
var tdHeader = document.getElementById("tableHeader").rows[0].cells;
var tdData = document.getElementById("tableData").rows[0].cells;
for (var i = 0; i < tdData.length; i++)
tdHeader[i].style.width = tdData[i].offsetWidth + 'px';
});
#2
9
Here is a jQuery plugin that does exactly that: http://fixedheadertable.com/
这是一个jQuery插件,它正是这样做的:http://fixedheadertable.com/
Usage:
用法:
$('selector').fixedHeaderTable({ fixedColumn: 1 });
$('selector')。fixedHeaderTable({fixedColumn:1});
Set the fixedColumn
option if you want any number of columns to be also fixed for horizontal scrolling.
如果要为水平滚动修复任意数量的列,请设置fixedColumn选项。
EDIT: This example http://www.datatables.net/examples/basic_init/scroll_y.html is much better in my opinion, although with DataTables you'll need to get a better understanding of how it works in general.
编辑:这个例子http://www.datatables.net/examples/basic_init/scroll_y.html在我看来要好得多,尽管使用DataTables你需要更好地了解它的工作原理。
EDIT2: For Bootstrap to work with DataTables you need to follow the instructions here: http://datatables.net/blog/Twitter_Bootstrap_2 (I have tested this and it works)- For Bootstrap 3 there's a discussion here: http://datatables.net/forums/discussion/comment/53462 - (I haven't tested this)
EDIT2:要使Bootstrap与DataTables一起使用,你需要按照这里的说明操作:http://datatables.net/blog/Twitter_Bootstrap_2(我已经测试了这个并且它可以工作) - 对于Bootstrap 3,这里有一个讨论:http:// datatables .net / forums / discussion / comment / 53462 - (我还没有测试过这个)
#3
4
Interesting question, I tried doing this by just doing a fixed position row, but this way seems to be a much better one. Source at bottom.
有趣的问题,我尝试通过只做一个固定的位置行来做这个,但这种方式似乎是一个更好的。来源于底部。
css
CSS
thead { display:block; background: green; margin:0px; cell-spacing:0px; left:0px; }
tbody { display:block; overflow:auto; height:100px; }
th { height:50px; width:80px; }
td { height:50px; width:80px; background:blue; margin:0px; cell-spacing:0px;}
html
HTML
<table>
<thead>
<tr><th>hey</th><th>ho</th></tr>
</thead>
<tbody>
<tr><td>test</td><td>test</td></tr>
<tr><td>test</td><td>test</td></tr>
<tr><td>test</td><td>test</td></tr>
</tbody>
http://www.imaputz.com/cssStuff/bigFourVersion.html
http://www.imaputz.com/cssStuff/bigFourVersion.html
#4
0
<table class="table table-striped table-condensed table-hover rsk-tbl vScrollTHead">
<thead>
<tr>
<th>Risk Element</th>
<th>Description</th>
<th>Risk Value</th>
<th> </th>
</tr>
</thead>
</table>
<div class="vScrollTable">
<table class="table table-striped table-condensed table-hover rsk-tbl vScrollTBody">
<tbody>
<tr class="">
<td>JEWELLERY</td>
<td>Jewellery business</td>
</tr><tr class="">
<td>NGO</td>
<td>none-governmental organizations</td>
</tr>
</tbody>
</table>
</div>
.vScrollTBody{
height:15px;
}
.vScrollTHead {
height:15px;
}
.vScrollTable{
height:100px;
overflow-y:scroll;
}
having two tables for head and body worked for me
有两张头和身体的桌子为我工作