简体   繁体   English

material-ui 表格多子行不能一起悬停

[英]material-ui table multi subrow can't be hovered together

I'm using table of material-ui, like this img我正在使用 material-ui 表,就像这个 img

When I hover the specific row, the background color will become dark, sometimes one row could have sub-row.当我 hover 特定行时,背景颜色会变暗,有时一行可以有子行。 I want the sub-row will become darker when I hover.我希望子行在 hover 时会变暗。

My design structure is like below, but this way only the selected tablecell being hovered, not all subRow's background color get darker.我的设计结构如下所示,但这样只有选定的表格单元被悬停,并非所有 subRow 的背景颜色都变暗。

I also tried wrappping sub tablecells of subrow into a tablerow/tablecell/div.我还尝试将 subrow 的子表格单元格包装到 tablerow/tablecell/div 中。 It doesn't work, is there a better way to achieve this?它不起作用,有没有更好的方法来实现这一点?

import { Table,TableHead,TableRow,TableCell,TableBody,Divider,makeStyles} from '@material-ui/core'
export default Table = () => {
const subRow=[{a:'test1',b:1},{a:'test2',b:2}]
const tableClasses = useStyles()
  return(


 <Table>
  <TableHead>
    <TableRow>
      <TableCell />
    </TableRow>
  </TableHead>
  <TableBody>
    <TableRow>
      <TableCell>WER</TableCell>
      <TableCell> 234</TableCell>

      <TableCell>
        {subRow.map((e, index) => {
          return (
            <div className={tableClasses.subRow}>
              <div> {e.a} </div>
              {index < subCells.length - 1 && <Divider />}
            </div>
          );
        })}
      </TableCell>
      <TableCell>
        {subRow.map((e, index) => {
          return (
            <div className={tableClasses.subRow}>
              <div> {e.a} </div>
              {index < subCells.length - 1 && <Divider />}
            </div>
          );
        })}
      </TableCell>
    </TableRow>
  </TableBody>
</Table>;


)
const useStyles = makeStyles(() => ({
 subRow:{
  '&:hover':{
    backgroundColor: 'red'
 }},
}))
}

You can't use a selector when declaring inline styles on an element, as by declaring it inline, you are already selecting this div to apply style.在元素上声明内联 styles 时不能使用选择器,因为通过内联声明它,您已经选择了此 div 来应用样式。 A proper inline style declaration looks like this -> <div style="background:red;"> .正确的内联样式声明如下所示 -> <div style="background:red;">
The limitations of this are that you can't use pseudo/state selectors for it like :hover .这样做的限制是您不能使用伪/状态选择器,例如:hover

To do that, you need to put your style code in a <style></style> block or a separate css file.为此,您需要将样式代码放在<style></style>块或单独的 css 文件中。

An example that assumes your code outputs to the browser as a standard HTML <table> element;假设您的代码作为标准 HTML <table>元素输出到浏览器的示例;

<style>
table tbody td:hover {background-color:red;}
/* or if you want to keep it to the div... */
table tbody td div:hover {background-color:red;}
</style>

Otherwise replace with whatever classes are used to create the table structure, if it doesn't look like it's working, also check that it's not getting overridden by more specific base UI styles.否则替换为用于创建表结构的任何类,如果它看起来不像在工作,还要检查它是否没有被更具体的基本 UI styles 覆盖。

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

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