reactjs - Material 表 rowStyle 背景颜色根据单元格值变化

标签 reactjs material-table

我试图让行的颜色根据优先级数字改变颜色。例如,如果优先级为 4,则整行的颜色应为蓝色。问题是我不确定如何实现这一目标。我知道 Material 表将 ID 分配给行,但我无法访问它们。以下是我到目前为止所想到的。我需要根据优先级数字设置选项[backgroundColor]。我这里也有一个codesandbox https://codesandbox.io/s/notifications-material-ui-spq45

import React from "react";
import { Container } from "react-bootstrap";
import MaterialTable from "material-table";
import FilterListIcon from "@material-ui/icons/FilterList";
import SearchIcon from "@material-ui/icons/Search";
import FirstPage from "@material-ui/icons/FirstPage";
import LastPage from "@material-ui/icons/LastPage";
import ChevronLeft from "@material-ui/icons/ChevronLeft";
import ChevronRight from "@material-ui/icons/ChevronRight";
import CloseIcon from "@material-ui/icons/Close";

function App() {

  //loop through the array of priority numbers, and display color based on number
  
  function colors(num) {

    for(let i = 0; i < num.length; i++) {
    if (num[i] === 4) {
      options.rowStyle.backgroundColor = "blue";
    }

    if (num[i] === 3) {
      options.rowStyle.backgroundColor = "red";
    }

    console.log(num[i]);
  }
}

  let data = [
    {
      date: "06-29-2021",
      subject: "CANBUS LOSS, Automatic Reboot!",
      message:
        "SCMs CANBUS LOSS for more than 15 min, Automatic System Reboot!",
      category: "System",
      priority: 4,
      error: "SYS0080"
    },

    {
      date: "06-28-2021",
      subject: "Reboot!",
      message: "Automatic System Reboot!",
      category: "Alarm",
      priority: 3,
      error: "SYS0090"
    },
    {
      date: "06-25-2021",
      subject: "Testing!",
      message: "Generator not running!",
      category: "Generator",
      priority: 2,
      error: "SYS0050"
    }
  ];

  let columns = [
    { title: "Date", field: "date" },
    { title: "Subject", field: "subject" },
    { title: "Message", field: "message" },
    { title: "Category", field: "category" },
    { title: "Priority Level", field: "priority" },
    { title: "Error Code", field: "error" }
  ];

  let options = {
    filtering: false,
    sorting: true,
    rowStyle: {
    fontFamily: "Mulish-Regular",
    backgroundColor: ""
    
    
       
     
    },
    headerStyle: {
      fontFamily: "Mulish-Regular",
      fontSize: "1.1em",
      fontWeight: "600",
      backgroundColor: "#D1D1D8"
    },
    searchFieldStyle: {
      fontFamily: "Mulish-Regular"
    }
  };
// Loop through all the data and find the priority number, and put it in an array

  let map = data.map((x) => x.priority);
  console.log(map);
  colors(map);

  return (
    <>
      <Container>
        <MaterialTable
          title=""
          columns={columns}
          data={data}
          options={options}
          icons={{
            Filter: (props) => <FilterListIcon style={{ fill: "#2D3155 " }} />,
            Search: (props) => <SearchIcon style={{ fill: "#2D3155 " }} />,
            FirstPage: (props) => <FirstPage style={{ fill: "#2D3155 " }} />,
            LastPage: (props) => <LastPage style={{ fill: "#2D3155 " }} />,
            NextPage: (props) => <ChevronRight style={{ fill: "#2D3155 " }} />,
            PreviousPage: (props) => (
              <ChevronLeft style={{ fill: "#2D3155 " }} />
            ),
            SortArrow: (props) => (
              <FilterListIcon
                style={{ fill: "#2D3155 ", fontSize: "1.4em", margin: ".4em" }}
              />
            ),
            ResetSearch: (props) => <CloseIcon style={{ fill: "#2D3155 " }} />
          }}
        />
      </Container>
    </>
  );
}

export default App;

最佳答案

正如您在 docs 中所读到的那样, rowStyle 接受一个对象或函数。

因此,您可以使用接收 rowData 作为参数的函数来设置 rowStyle,示例:

const rowBackgroundColors = {
  "2": "yellow", // just for example, remove it if you don't need
  "3": "orange",
  "4": "red",
};

const options = {
  // ...
  rowStyle: (rowData) => {
    return {
      fontFamily: "Mulish-Regular",
      backgroundColor: rowBackgroundColors[rowData.priority] ?? "#fff",
    };
  },
  // ...
};

关于reactjs - Material 表 rowStyle 背景颜色根据单元格值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68320401/

相关文章:

javascript - 单击链接后无法渲染 react 组件

javascript - TypeScript 中的条件泛型类型使用可区分联合

javascript - 如何更改 onRowAdd、onRowUpdate、onRowDelete 的 Material 表图标的颜色?

html - 根据条件删除 detailPanel 导致填充

javascript - 如何使 Material 表中只有 1 行可编辑?

javascript - 优化 Canvas 画圆

javascript - React 的不可变元素创建和渲染机制不是效率极低吗?

node.js - 创建 React 应用程序入门 - "Cannot find module"

css - 如何样式化(添加 css)到 Material 表(React)?