简体   繁体   English

如何使用一系列颜色为 MUI 数据网格的单元格着色?

[英]How to color the cells of MUI Data Grid with a range of color?

The MUI examples only teach how to choose between a defined number of colors. I would like to be able to choose among all the possible colors. MUI 示例只教如何在定义的数字 colors 之间进行选择。我希望能够在所有可能的 colors 中进行选择。

Example: We have a cell value between 0 and 100. Each value could be converted to a color thanks to a given function. Then, the color is applied to the color.示例:我们有一个介于 0 和 100 之间的单元格值。由于给定的 function,每个值都可以转换为一种颜色。然后,将颜色应用于颜色。

How would you proceed please?请问您将如何进行?

Solution from @jacob smith is working:来自@jacob smith 的解决方案正在运行:

Use the renderCell to wrap your value with a div or similar and style that as you need, you should be able to use this in combination with cellClassName to get the div to fill the whole cell.使用 renderCell 将您的值包装为 div 或类似样式,并根据需要设置样式,您应该能够将其与 cellClassName 结合使用以使 div 填充整个单元格。

You would need something similar to the following in the column definition:您需要在列定义中使用类似于以下内容的内容:

cellClassName: (params: GridCellParams<number>) => {
      return "super-app";
    },
    renderCell: (params: GridRenderCellParams) => {
      const color = getColor(params.value);

      return (
        <Box
          sx={{
            backgroundColor: color,
            width: "100%",
            height: "100%"
          }}
        >
          {params.value}
        </Box>
      );
    }

Here is a codesandbox example 这是一个codesandbox示例

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

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