简体   繁体   English

在Scroll的Handsontable网格行上应用的CSS无法正常工作

[英]CSS applied on Handsontable grid row on Scroll is not working properly

I am using Handsontable 0.11.4 grid where the rows on load have yellow background, on click of icon in row there is a logic to remove yellow background and it works perfectly. 我正在使用Handsontable 0.11.4网格,其中加载的行具有黄色背景,单击该行中的图标时,可以删除黄色背景,并且该逻辑可以完美运行。

if I click on 2 rows it sets those rows white, as it should. 如果我单击2行,则应将这些行设置为白色。 when I scroll down the white rows scroll with it. 当我向下滚动时,白色行随之滚动。 when you scroll back up it returns to the originally selected row 当您向上滚动时,它将返回到最初选择的行

jsfiddle  - `https://jsfiddle.net/3ukL2yvt/`

Steps to reproduce - 重现步骤 -

1)Click on icon in row 1,2 etc. It will become white 2)Scroll down 1)单击第1,2行等中的图标,它将变为白色2)向下滚动

Every 1,2 etc row after scroll is having white background now(seems handsontable is using index behind the scenes on scroll). 滚动后的每1,2等行现在都具有白色背景(似乎handsontable正在使用滚动幕后的索引)。 Is there any way to fix it? 有什么办法可以解决?

Any help would be really appreciated. 任何帮助将非常感激。

This is unfortunately the expected behavior and here is why. 不幸的是,这是预期的行为,这就是原因。 What you are seeing is two of the features Handsontable offers: Virtual Rendering and Stateless HTML. 您将看到Handsontable提供的两个功能:虚拟渲染和无状态HTML。

Virtual Rendering 虚拟渲染

This feature is used extensively to achieve fast table rendering when your data contains thousands of rows. 当您的数据包含数千行时,广泛使用此功能来实现快速的表呈现。 Instead of rendering the entire table to the DOM, it simply renders what you can see plus a few more rows. 而不是将整个表呈现给DOM,它只是呈现您所看到的内容以及更多的行。 As you scroll, it renders this rolling window. 滚动时,它将呈现此滚动窗口。 This leads to the second point which is a stateless DOM. 这导致第二点是无状态DOM。

Stateless DOM 无状态DOM

Handonstable adopts the idea of keeping a DOM which contains minimal information and is just a reflection of its data objects. Handonstable采用保持DOM的思想,该DOM包含的信息最少,只是其数据对象的反映。 To this end, it renders fairly often. 为此,它经常渲染。 So, as opposed to messing with the DOM elements to, say, move a row from position 1 to position 2, it simply swaps these two rows in its data objects and re-renders itself. 因此,与弄乱DOM元素(例如,将行从位置1移动到位置2)相对,它只是在其数据对象中交换这两行并重新呈现自己。

What this means is that any changes you make to the table using CSS or JS will get wiped when the table is re-rendered. 这意味着重新渲染表时,使用CSS或JS对表所做的任何更改都会被擦除。 This happens when you scroll since the virtual renderer will eventually have to re-render a new section of your table. 滚动时会发生这种情况,因为虚拟渲染器最终将不得不重新渲染表的新部分。

Solution

Of course there are many ways to achieve your desired result and here is the most common: 当然,有很多方法可以达到您想要的结果,这是最常见的方法:

To your disposition is are customRenderers . 您的配置是customRenderers These are functions that can be applied to individual cells or columns through the columns or cells options upon initialization. 这些功能可以在初始化时通过columnscells选项应用于单个单元格或列。 Here is the example on the docs page: 这是docs页面上的示例:

function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
    Handsontable.renderers.TextRenderer.apply(this, arguments);
    td.style.fontWeight = 'bold';
    td.style.color = 'green';
    td.style.background = '#CEC';
 }

What you see is that if we applied this renderer to all cells, then it would render them as Text , and give them all of those CSS rules. 您所看到的是,如果我们将此渲染器应用于所有单元格,那么它将把它们渲染为Text ,并为它们提供所有这些CSS规则。

In your case, you could have your click event add the [row,col] coordinates by using hotInstance.getSelected() to some array, let's call it clickedCells . 在您的情况下,您可以通过使用hotInstance.getSelected()将click事件添加[row,col]坐标到某个数组,我们将其clickedCells Then in your custom renderer, you would have a conditional that says if the row and column are in clickedCells , render with whatever CSS you want. 然后在您的自定义渲染器中,您将有一个条件,说明行和列是否在clickedCells ,并使用所需的CSS进行渲染。

That should be it! 就是这样!

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

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