简体   繁体   English

HTML表带有粘性页眉,页脚,列和可滚动的正文(X和Y)

[英]HTML table with sticky header, footer, column AND scrollable tbody (X and Y)

I'm developing HTML table (inside React application) with a whole range of sticky elements: header, footer, first column (I'm using position: sticky; to make it works). 我正在开发具有一系列粘性元素的HTML表(在React应用程序内部):标题,页脚,第一列(我正在使用position: sticky;以使其起作用)。 Table is closed inside container with known dimensions, and allow user to scroll in X and Y directions (see my JSFiddle https://jsfiddle.net/aypLr936/ - it's only the mock up). 桌子被封闭在具有已知尺寸的容器内,并且允许用户在X和Y方向上滚动(请参阅我的JSFiddle https://jsfiddle.net/aypLr936/-仅是模型)。 Currently I'm trying to 'migrate' with scrolling area (X and Y) inside tbody (something like https://datatables.net/examples/basic_init/scroll_xy.html ). 目前,我正在尝试使用tbody中的滚动区域(X和Y) “迁移”(类似https://datatables.net/examples/basic_init/scroll_xy.html )。 I've tried to implement solutions from HTML table with 100% width, with vertical scroll inside tbody but it doesn't work in my conditions. 我试图从宽度为100%的HTML表中实现解决方案,并在tbody内进行垂直滚动,但这在我的情况下不起作用。 I'm just wondering if it possible to make it works without breaking existing functionality (table has also an option to resize columns, which must work after changes, resizable columns must overflow parent container [not resize sibling columns]). 我只是想知道是否有可能在不破坏现有功能的情况下使其工作(表还具有调整列大小的选项,该选项必须在更改后起作用,可调整大小的列必须溢出父容器[不能调整同级列])。

One other approach I tried is to separate table into 3: one for thead, one for tbody, one for tfoot. 我尝试过的另一种方法是将桌子分成3个:一个用于thead,一个用于tbody,一个用于tfoot。 But in this case there is an issue with horizontal scroll (thead and tfoot don't follow tbody position). 但是在这种情况下,水平滚动会出现问题(脚和脚不跟着身体的位置)。 Maybe is there any way to create linkage between different table scroll position? 也许有什么办法可以在不同的表格滚动位置之间建立联系?

I will be very grateful for all tips, suggestions, cheers J. 我将非常感谢所有提示,建议和欢呼声J。

Update: I found solution which satisfy me for this very moment. 更新:目前我找到了令我满意的解决方案。 Actually there is a way to create two separate divs with table parts and couple them by scroll position (it can be done by pure JS). 实际上,有一种方法可以创建带有表部件的两个单独的div,并通过滚动位置将它们耦合(可以通过纯JS完成)。 I think it's similiar solution to https://datatables.net/examples/basic_init/scroll_xy.html . 我认为这是https://datatables.net/examples/basic_init/scroll_xy.html的类似解决方案。 I developed something like this (inside my React app): 我在React应用程序内部开发了如下代码:

<div id="thead_scroll" style={{overflowX: 'hidden'}}>
  <table>
    <thead> 
       ...
    </thead>
  </table>
</div>
<div id="tbody_scroll" style={{overflowX: 'auto', maxHeight: 500}} onScroll={this.handleBodyScroll}>
  <table>
    <tbody> 
       ...
    </tbody>
  </table>
</div>

Event is handled in this way: 事件的处理方式如下:

handleBodyScroll(event) {
  const tbody = document.getElementById('tbody_scroll');
  const thead = document.getElementById('thead_scroll');
  thead.scrollLeft = tbody.scrollLeft;
}

We are operating directly on DOM and scrolling looks smoothly. 我们直接在DOM上运行,滚动看起来很流畅。 In this solution header is always above tbody, another elements may be sticky . 在此解决方案中,标题始终位于tbody上方,其他元素可能会发sticky One last issue I've noticed is scrollbar position which makes minor displacement in extreme position (max X), but I hope it can be solved by some measurement... 我注意到的最后一个问题是滚动条位置,该滚动条位置会在极限位置(最大X)产生较小的位移,但我希望可以通过一些测量来解决...

the other day I saw exactly what you want, a "Table with fluid height and width, fixed header, footer and first column using position:sticky" that is pure css: 前几天,我确切地看到了您想要的东西,一个纯css的“具有高度和宽度,固定的页眉,页脚和第一列的表格,使用position:sticky”

https://codepen.io/paulobrien/pen/LBrMxa https://codepen.io/paulobrien/pen/LBrMxa

Quote: This example uses position:sticky on the th elements in the thead, tfoot and left column to achieve the fixed effect. Quote:此示例在thead,tfoot和left列中的th个元素上使用position:sticky来实现固定效果。 ... Resize browser smaller to see fixed first column. ...将浏览器的大小调整为较小以查看固定的第一列。

I can't copy the whole pen so you have to look at it your self and make your own stand alone to play with. 我无法复制整支笔,因此您必须自己看一下,并自己动手玩。

Then, to also get the column resize in that pen, just add the resize and overflow in this ruleblock at line 38 - 43: 然后,要获得该笔的列调整大小,只需在此规则块的第38-43行中添加调整大小和溢出:

.table-scroll thead th {
  background: #333;
  color: #fff;
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  resize: horizontal; /* add */
  overflow: auto; /* add */
}

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

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