简体   繁体   English

如何独立于“thead”滚动表格的“tbody”?

[英]How to scroll table's “tbody” independent of “thead”?

As specified in the W3 specification for Tables: 如表W3规范中所指定:

Table rows may be grouped into a table head, table foot, and one or more table body sections, using the THEAD , TFOOT and TBODY elements, respectively. 表行可以分别使用THEADTFOOTTBODY元素分组为表头,表脚和一个或多个表体部分。 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: HTML:

<table>
    <thead>
        <tr>
            <td>Problem</td>
            <td>Solution</td>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

JS: 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: CSS:

table {
    background-color: #aaa;
}
tbody {
    background-color: #ddd;
    height: 100px;
    overflow: auto;
}
td {
    padding: 3px 10px;
}

The missing part is: 缺少的部分是:

thead, tbody {
    display: block;
}

Demo 演示

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). 我需要在ui对话框内的桌子上进行y轴滚动(是的,你听到我说对了)。 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. 将顶级元素(thead,tfoot和tbody)设置为显示块的问题在于,各个组件之间的列大小的浏览器同步很快就会丢失,并且所有内容都打包到最小的允许大小。 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. 如果发生对表宽度的更改,大多数主流浏览器将相应地自动缩放tbody列宽。 All that's left is to set the header and footer column widths from their respective tbody sizes. 剩下的就是从它们各自的tbody大小设置页眉和页脚列宽。

$table.find("> thead,> tfoot").find("> tr:first-child")
    .each(function(i,e) {
        $(e).children().each(function(i,e) {
            if (i != column_scaled_widths.length - 1) {
                $(e).width(column_scaled_widths[i] - ($(e).outerWidth() - $(e).width()));
            } else {
                $(e).width(column_scaled_widths[i] - ($(e).outerWidth() - $(e).width()) + $.position.scrollbarWidth());
            }
        });
    });

This fiddle illustrates these notions: http://jsfiddle.net/borgboyone/gbkbhngq/ . 这个小提琴说明了这些概念: http//jsfiddle.net/borgboyone/gbkbhngq/

Note that a table wrapper or additional tables are not needed for y-axis scrolling alone. 请注意,单独的y轴滚动不需要表包装器或其他表。 (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. (X轴滚动确实需要一个包装表。)如果遇到标题或主体列的最小包大小,主体和标题的列大小之间的同步仍将丢失。 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/ 从这个出发点的最终结果在这里完全实现: http//borgboyone.github.io/jquery-ui-table/

A. 一个。

try this. 试试这个。

table 
{
    background-color: #aaa;
}

tbody 
{
    background-color: #ddd;
    height: 100px;
    overflow-y: scroll;
    position: absolute;
}

td 
{
    padding: 3px 10px;
    color: green;
    width: 100px;
}
thead {
  position: fixed;
  height: 10px; /* This is whatever height you want */
}
  tbody {
  position: fixed;
  margin-top: 10px; /* This has to match the height of thead */
  height: 300px; /* This is whatever height you want */
}

mandatory parts: 强制部分:

tbody {
    overflow-y: scroll;  (could be: 'overflow: scroll' for the two axes)
    display: block;
    with: xxx (a number or 100%)
}

thead {
    display: inline-block;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM