4

我使用 React.js 和MUI-Datatables创建了一个简单的表:

import MUIDataTable from "mui-datatables";

const columns = ["Name", "Company", "City", "State"];

const data = [
 ["Joe James", "Test Corp", "Yonkers", "NY"],
 ["John Walsh", "Test Corp", "Hartford", "CT"],
 ["Bob Herm", "Test Corp", "Tampa", "FL"],
 ["James Houston", "Test Corp", "Dallas", "TX"],
];

const options = {
  filterType: 'checkbox',
};

<MUIDataTable
  title={"Employee List"}
  data={data}
  columns={columns}
  options={options}
/>

如何将自定义 CSS 类添加到表内的单行。假设我希望 John Walsh 的第二行具有绿色背景色。

更新:

使用customRowRender允许设置特定行的样式,但我仍然对解决方案不是 100% 满意,因为可选行等某些功能不再开箱即用:

const options = {
    filterType: "checkbox",
    customRowRender: (data, dataIndex, rowIndex) => {
      let style = {};
      if (data[0] === "John Walsh") {
        style.backgroundColor = "green";
      }
      return (
        <TableRow style={style}>
          <TableCell />
          <TableCell>
            <Typography>{data[0]}</Typography>
          </TableCell>
          <TableCell>
            <Typography>{data[1]}</Typography>
          </TableCell>
          <TableCell>
            <Typography>{data[2]}</Typography>
          </TableCell>
          <TableCell>
            <Typography>{data[3]}</Typography>
          </TableCell>
        </TableRow>
      );
    }
  };
4

2 回答 2

7

干得好。

setRowProps: row => { 
        if (row[0] === "New") {
          return {
            style: { background: "snow" }
          };
        }
      }
于 2020-03-19T02:42:51.813 回答
1

您可以选择使用customRowRender并为选定的行提供您自己的样式。由于它们都是不同的组件,因此它们可以使用道具、上下文或某种状态管理来相互“交谈”。

于 2019-12-01T13:48:09.503 回答