如何滚动表格的“正文”独立于“头部”?
正如表的 W3 规范中所指定:
表格行可以分为桌头、桌脚和一个或多个 更多表格主体部分,使用
THEAD
,TFOOT
和TBODY
元素, 分别。这种划分使用户代理能够支持滚动 独立于桌头和桌脚的桌体。
我创建了以下示例,但它不起作用。
HTML:
<table>
<thead>
<tr>
<td>Problem</td>
<td>Solution</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS:
$(function() {
for (var i = 0; i < 20; i++) {
var a = Math.floor(10 * Math.random());
var b = Math.floor(10 * Math.random());
var row = $("<tr>").append($("<td>").html(a + " + " + b + " ="))
.append($("<td>").html(a + b));
$("tbody").append(row);
}
});
CSS:
table {
background-color: #aaa;
}
tbody {
background-color: #ddd;
height: 100px;
overflow: auto;
}
td {
padding: 3px 10px;
}
As specified in the W3 specification for Tables:
Table rows may be grouped into a table head, table foot, and one or
more table body sections, using theTHEAD
,TFOOT
andTBODY
elements,
respectively. This division enables user agents to support scrolling
of table bodies independently of the table head and foot.
I created the following example, but it doesn't work.
HTML:
<table>
<thead>
<tr>
<td>Problem</td>
<td>Solution</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
JS:
$(function() {
for (var i = 0; i < 20; i++) {
var a = Math.floor(10 * Math.random());
var b = Math.floor(10 * Math.random());
var row = $("<tr>").append($("<td>").html(a + " + " + b + " ="))
.append($("<td>").html(a + b));
$("tbody").append(row);
}
});
CSS:
table {
background-color: #aaa;
}
tbody {
background-color: #ddd;
height: 100px;
overflow: auto;
}
td {
padding: 3px 10px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
缺少的部分是:
演示
The missing part is:
Demo
大约一个月前,当我遇到类似问题时,我看到了这篇文章。我需要 y 轴滚动 ui 对话框内的表格(是的,你没听错)。我很幸运,因为一个可行的解决方案很快就出现了。然而,没过多久,该解决方案就获得了自己的生命,但随后又出现了更多内容。
仅将顶级元素(thead、tfoot 和 tbody)设置为显示块的问题是,各个组件之间的列大小的浏览器同步很快就会丢失,并且所有内容都会打包到最小允许的大小。设置列的宽度似乎是最好的做法,但是如果没有将所有内部表格组件的宽度设置为与这些列的总和相匹配,即使使用固定的表格布局,标题和正文之间也会略有不同当存在滚动条时。
我的解决方案是设置所有宽度,检查是否存在滚动条,然后采用浏览器实际决定的缩放宽度,并将它们复制到页眉和页脚,调整最后一列的宽度以适应滚动条的大小滚动条。这样做可以为列宽提供一定的流动性。如果表格宽度发生变化,大多数主要浏览器将相应地自动缩放 tbody 列宽度。剩下的就是根据各自的正文大小设置页眉和页脚列的宽度。
这个小提琴说明了这些概念:http://jsfiddle.net/borgboyone/gbkbhngq/。
请注意,仅 y 轴滚动不需要表包装器或附加表。 (X 轴滚动确实需要包装表。)如果遇到标题或正文列的最小包大小,正文和标题的列大小之间的同步仍然会丢失。如果可以选择调整大小或需要较小的表格宽度,则应提供最小宽度的机制。
从这个起点开始的最终高潮在这里得到了充分实现: http://borgboyone.github.io/ jquery-ui-table/
A.
I saw this post about a month ago when I was having similar problems. I needed y-axis scrolling for a table inside of a ui dialog (yes, you heard me right). I was lucky, in that a working solution presented itself fairly quickly. However, it wasn't long before the solution took on a life of its own, but more on that later.
The problem with just setting the top level elements (thead, tfoot, and tbody) to display block, is that browser synchronization of the column sizes between the various components is quickly lost and everything packs to the smallest permissible size. Setting the widths of the columns seems like the best course of action, but without setting the widths of all the internal table components to match the total of these columns, even with a fixed table layout, there is a slight divergence between the headers and body when a scroll bar is present.
The solution for me was to set all the widths, check if a scroll bar was present, and then take the scaled widths the browser had actually decided on, and copy those to the header and footer adjusting the last column width for the size of the scroll bar. Doing this provides some fluidity to the column widths. If changes to the table's width occur, most major browsers will auto-scale the tbody column widths accordingly. All that's left is to set the header and footer column widths from their respective tbody sizes.
This fiddle illustrates these notions: http://jsfiddle.net/borgboyone/gbkbhngq/.
Note that a table wrapper or additional tables are not needed for y-axis scrolling alone. (X-axis scrolling does require a wrapping table.) Synchronization between the column sizes for the body and header will still be lost if the minimum pack size for either the header or body columns is encountered. A mechanism for minimum widths should be provided if resizing is an option or small table widths are expected.
The ultimate culmination from this starting point is fully realized here: http://borgboyone.github.io/jquery-ui-table/
A.
试试这个。
try this.
强制性部分:
mandatory parts: