我使用 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>
);
}
};