简体   繁体   English

反应材料表:数据树中的颜色行

[英]React material-table: Color rows in data-tree

I am using material-table in my React project.我在我的 React 项目中使用材料表。 I have 3 levels of the data tree.我有 3 个级别的数据树。 Here it is the first one:这是第一个:

在此处输入图像描述

Is it possible when I click the first of the 2 items on 1st Level in data tree table to color it so it would be easier to see that values under it are child elements.当我单击数据树表中第一级的 2 个项目中的第一个对其进行着色时,是否可以更容易地看到它下面的值是子元素。 Like this:像这样: 在此处输入图像描述

Also I would be even happier if it is possible to color it when I am passing data to it.此外,如果在我将数据传递给它时可以为它着色,我会更高兴。 Here it is how I am passing data:这是我传递数据的方式:

data={[
        {
          id: 1, // MAIN ELEMENT 
          name: "Parent",
          value: `Parent`,
        },
        {
          id: 2, //CHILD OF THE MAIN ELEMENT
          name: "Child",
          value: `Child`,
          parentId: 1,
        }]}

Is there an option to color parent Element even before opening it, so it would be clear that it is PARENT and other is CHILD?甚至在打开父元素之前是否有为父元素着色的选项,所以很明显它是父元素而其他是子元素?

UPDATE:更新:

Here is codesandbox example.这是代码沙盒示例。 As you can see when you open Parent1 Parent2 seems to be under Parent1.正如您在打开 Parent1 时所看到的,Parent2 似乎在 Parent1 之下。 I want to make it clear that it is NOT under it.我想明确表示它不在它之下。

https://codesandbox.io/s/jolly-germain-6fncr?file=/src/App.js https://codesandbox.io/s/jolly-germain-6fncr?file=/src/App.js

Let we talk about this problem first.我们先来谈谈这个问题。 It's neither programmatic nor css problem.这既不是程序问题,也不是 css 问题。 It's just the problem how you show data, in other words, UX only.这只是你如何显示数据的问题,换句话说,只是用户体验。

There are several ways to achive, this is my work example: https://codesandbox.io/s/withered-dust-hb882?file=/src/App.js有几种方法可以实现,这是我的工作示例: https://codesandbox.io/s/withered-dust-hb882?file=/src/App.js

Basically I just add one first column for parent only, that's it.基本上我只是为父级添加一个第一列,就是这样。

Ok, using CSS selectors it is not so easy to implement onExapnd color change.好的,使用 CSS 选择器实现 onExapnd 颜色更改并不容易。 Here you will have to write check for parent TR check and sub Button rotate(90deg) check.在这里,您必须为父 TR 检查和子按钮旋转(90 度)检查编写检查。 To change the colors without onClick check you can use the following CSS:要在没有 onClick 检查的情况下更改 colors,您可以使用以下 CSS:

tr[level="0"] {
  background-color: #FF0000;
}

tr[level="1"] {
  background-color: #FF0033;
}

tr[level="2"] {
  background-color: #FF0066;
}

In JS way you can use the following code (of course you will have to add it in every table or extend the table or use util lib with ready rowStyle method..)在 JS 方式中,您可以使用以下代码(当然,您必须将其添加到每个表中或扩展表或使用带有现成 rowStyle 方法的 util lib ..)

import React from "react";
import MaterialTable from "material-table";
import SearchIcon from "@material-ui/icons/Search";
import RotateLeftIcon from "@material-ui/icons/RotateLeft";
import { ArrowUpward, ChevronRight } from "@material-ui/icons";

//import './styles.css';

export default () => {
  const constPathColors = {
    1: '#FFFF00',
    2: '#FFFF33',
    3: '#FFFF66',
    4: '#FFFF99',
    5: '#FFFFCC'
  };
  return (
    <MaterialTable
      style={{ width: "100%", margin: "3%" }}
      title="Income Statement"
      icons={{
        Filter: React.forwardRef((props, ref) => <SearchIcon ref={ref} />),
        Search: React.forwardRef((props, ref) => <SearchIcon ref={ref} />),
        ResetSearch: React.forwardRef((props, ref) => (
          <RotateLeftIcon ref={ref} />
        )),
        SortArrow: ArrowUpward,
        DetailPanel: ChevronRight
      }}
      columns={[
        {
          field: "name",
          title: "Category"
        },
        {
          field: "value",
          title: "Value",

          cellStyle: {
            textAlign: "center"
          }
        }
      ]}
      data={[
        {
          id: 1, // MAIN ELEMENT
          name: "Parent 1",
          value: `SomeParentValue`
        },
        {
          id: 2, //CHILD OF THE MAIN ELEMENT
          name: "Child 1-1",
          value: `Child Value`,
          parentId: 1
        },
        {
          id: 3, //CHILD OF THE MAIN ELEMENT
          name: "Child 1-2",
          value: `Child Value`,
          parentId: 1
        },
        {
          id: 4, //CHILD OF THE CHILD ELEMENT
          name: "Child 1-2-1",
          value: `Child Value`,
          parentId: 3
        },
        {
          id: 5, // MAIN ELEMENT
          name: "Parent 2",
          value: `SomeParentValue`
        }
      ]}
      parentChildData={(row, rows) => rows.find(a => a.id === row.parentId)}
      options={{
        paging: false,
        headerStyle: {
          backgroundColor: "#378FC3",
          color: "#FFF",
          fontSize: "17px",
          textAlign: "center",
          fontWeight: "bold"
        },
        rowStyle: rowData => {
          if(rowData.tableData.isTreeExpanded === false && rowData.tableData.path.length === 1) {
            return {};
          }
          const rowBackgroundColor = constPathColors[rowData.tableData.path.length];
          return {backgroundColor: rowBackgroundColor};
        }
      }}
    />
  );
};

The row has default color before the expanding:该行在展开之前具有默认颜色:

在此处输入图像描述

After expanding it has yellow (gradient depend on level) background color:展开后它有黄色(渐变取决于级别)背景颜色:

在此处输入图像描述

Thats how my tree view looks like.这就是我的树视图的样子。 Thanks to the left: `var(--left-before, ${0}px) , i could positioning the:befores wherever i want感谢left: `var(--left-before, ${0}px) ,我可以将:befores 定位在我想要的任何地方

https://i.ibb.co/Wp9XJcc/childscapture.png https://i.ibb.co/Wp9XJcc/childscapture.png

viewTableTree.styles.js viewTableTree.styles.js

import { makeStyles } from '@material-ui/core/styles';

export const useViewTableTreeStyles = makeStyles(theme => ({
  root: {
    '& .MuiPaper-root': {
      boxShadow: 'none'
    },
    '& .MuiTable-root': {
      position: 'relative',
      overflow: 'hidden'
    },
    '& .MuiTableRow-root': {
      '&:hover': { backgroundColor: 'rgba(0, 0, 0, 0.04)' },
      '&:before': {
        content: '""',
        fontWeight: theme.font.weight.black,
        fontSize: theme.font.size.xxl,
        position: 'absolute',
        left: `var(--left-before, ${0}px)`,  //important trick here!
        width: '1px',
        height: '3.2rem',
        backgroundColor: theme.palette.basic.bright
      }
    }
  }
}));

then in the MaterialTable component然后在 MaterialTable 组件中

ViewTableTree.js ViewTableTree.js

  <div className={classes.root}>
      <MaterialTable
        icons={tableIcons}
        data={rows}
        columns={cells}
        localization={{
          header: {
            actions: ''
          }
        }}
        options={{
          selection: false,
          paging: false,
          search: false,
          showTitle: false,
          toolbar: false,
          actionsColumnIndex: -1,
          rowStyle: rowData => {
            let styles = { transition: 'transform 300ms' };
            const levels = rowData.tableData.path.length === 1 ? 0 : rowData.tableData.path.length;
            styles = { ...styles, '--left-before': `${levels * 6}px` };

            return rowData.tableData.isTreeExpanded
              ? {
                  ...styles,
                  fontWeight: 600,
                  backgroundColor: 'rgba(77, 93, 241, 0.08)'
                }
              : styles;
          }
        }}
        {...props}
      />
    </div>

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

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