【发布时间】:2020-03-10 09:08:49
【问题描述】:
我使用 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>
);
}
};
【问题讨论】:
-
我查看了他们的自述文件。我看到github.com/gregnb/mui-datatables#customize-styling 或
customRowRender函数可能会有所帮助。检查它们。 -
感谢您的评论。自定义样式不起作用,因为我不想为所有行设置样式。
customRowRender可能有效,但据我所知,使用时我需要自己渲染每个单元格。 -
也许您可以为行索引添加一个 if 语句。我不知道reactjs,但角度上有这样的功能
标签: javascript reactjs material-ui mui-datatable