简体   繁体   English

在 reactjs 中,在 hover 上,表格的一行应该更改具有相同行索引值的其他表格的行的背景

[英]In reactjs, On hover a row of the table should change background of row of other table with same row index value

I have two different tables, I need to highlight a row of the second table if the user hovers a row of the first table.我有两个不同的表,如果用户悬停第一个表的一行,我需要突出显示第二个表的一行。 The highlight must be done based on the hovered row index.突出显示必须基于悬停的行索引。

For example:例如:

<div>
    <table>
        <tr><td>table 1 row 1</td></tr> // => hovered
        <tr><td>table 1 row 2</td></tr>
        <tr><td>table 1 row 3</td></tr>
    </table>
</div>
<div>
    <table>
        <tr><td>table 2 row 1</td></tr> // => highlighted
        <tr><td>table 2 row 2</td></tr>
        <tr><td>table 2 row 3</td></tr>
    </table>
</div>

In the first table add an onMouseEnter/onMouseLeave event allowing you to set a state with the indice for the row over在第一个表中添加一个 onMouseEnter/onMouseLeave 事件,允许您设置一个 state 与该行的索引

<tr onMouseEnter={() => setRowToHover(1)}><td>table 1 row 1</td></tr>
<tr><td>table 2 row 2</td></tr>

In the second table dynamic add a className to the tr to hightlight if you are using a simple tr or add a hightlight props if you have a custom component在第二个表中,如果您使用简单的 tr,则动态将 className 添加到 tr 以突出显示;如果您有自定义组件,则添加 hightlight 道具

<tr className={rowToHover === 1 ? 'hover-class' : ''} ><td>table 2 row 1</td></tr>

I assume you're gonna render your array in a dynamic way我假设您将以动态方式呈现您的数组

add class name for each row为每一行添加 class 名称

<table>
  <tr class="table-row"><td>table 2 row 1</td></tr> // => highlighted
  <tr class="table-row"><td>table 2 row 2</td></tr>
  <tr class="table-row"><td>table 2 row 3</td></tr>
</table>

in scss file, (pseudo selector)在 scss 文件中,(伪选择器)

.table-row:hover {
  background-color: 'lightblue',
}

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

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