简体   繁体   English

我们如何冻结html表中的第一列和标题(具有多行)

[英]How can we freeze first column and header(which have multiple rows) in html table

I have tried so many solutions to freeze first column and header(which have multiple rows) but in every solution i have to user custom css (css according to solutions). 我已经尝试了很多解决方案来冻结第一列和标题(具有多行),但是在每个解决方案中,我都必须使用用户自定义CSS(根据解决方案的CSS)。

In my case I cannot change my previous css. 就我而言,我无法更改以前的CSS。 I want code in which on div scroll I can freeze my table(which is dynamic). 我想要在div滚动中可以冻结表(动态的)的代码。

Please provide solution. 请提供解决方案。

I've done this like this: 我这样做是这样的:

<table style="table-layout: fixed">
    <tr>
        <td><div style="width:100px; position:fixed;">Frozen Header text</div></td>
    </tr>
    <tr>
        <td>Some data</td>
    </tr>
<table>

I am not sure how to get the first column frozen as well, but I suppose you can attempt to use the same strategy, as well as formatting using the <col> tag, similar to this: 我不确定如何也冻结第一列,但我想您可以尝试使用相同的策略,以及使用<col>标记进行格式化,类似于:

<col style="width:100px; position:fixed;" />

Try that, and please let us know how you go. 尝试一下,请让我们知道您的情况。

To achieve what I comets jquery and css use. 为了实现我的彗星jQuery和CSS使用。 Here the plugin: link 这里的插件: 链接

This is a jQuery plugin that can make table rows and columns not scroll. 这是一个jQuery插件,可以使表的行和列不滚动。

It can take a given HTML table object and set it so it can freeze a given number of columns or rows or both, so the fixed columns or rows do not scroll. 它可以采用给定的HTML表对象并进行设置,以便冻结给定数量的列或行或同时冻结这两者,因此固定的列或行不会滚动。

The rows to be frozen should be placed in the table head section. 要冻结的行应放在表头部分。

It can also freeze rows and columns combined with using colspan or rowspan attributes. 它还可以冻结行和列,并结合使用colspan或rowspan属性。

Fix header is relative simple and easy to implement. Fix标头相对简单,易于实现。 and column is a different thing. 和专栏是另一回事。

for example: 例如:

<table>
  <tr class="header"><td>header</td></tr>
  <tr><td>body</td></tr>
</table>

You have to monitor onscroll envent, and detect if the header is out of view. 您必须监视onscroll envent,并检测标题是否在视野之外。 When it's out of view, retrieve tr.header's size parameters (also include TDs inside), and its clientX and clientY by using getBoundingClientRect(). 当它不可见时,使用getBoundingClientRect()检索tr.header的大小参数(还包括TD)及其clientX和clientY。 make a clone of tr.header by using cloneElement(true) and place it in the same table. 通过使用cloneElement(true)创建tr.header的副本,并将其放置在同一表中。 then assign size parameters to its TDs, and make this cloned element fixed to screen. 然后将大小参数分配给它的TD,并使此克隆的元素固定在屏幕上。

This solution won't not affect your table's styles or layout, while it is complex indeed. 尽管确实很复杂,但此解决方案不会影响表的样式或布局。

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

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