gpt4 book ai didi

reactjs - 我需要在 Material-ui DataGrid 中渲染 JSX 元素

转载 作者:行者123 更新时间:2023-12-03 08:22:58 31 4
gpt4 key购买 nike

我需要通过存储在 Object 中的给定字符串有条件地渲染样式化的 div。我传递数据对象 throw props 并通过此函数 convertOrdersRows 对其进行转换。但它给了我错误 TypeError: Converting Circle Structure to JSON --> Start at object with constructor 'Object' |属性“_context”->带有构造函数“Object”的对象

const orderColumns = [
{ field: "id", headerName: "ID", width: 120 },
{ field: "date", headerName: "Date", width: 140 },
{ field: "customerName", headerName: "Customer Name", width: 190 },
{ field: "products", headerName: "Products", width: 150 },
{ field: "price", headerName: "Price", width: 90 },
{ field: "status", headerName: "Status", width: 120 },
];

const productColumns = [];

const convertToNormalDate = (date) => {
const newDate = new Date(date);
const year = newDate.getFullYear();
let month = newDate.getMonth() + 1;
let dt = newDate.getDate();

if (dt < 10) {
dt = "0" + dt;
}
if (month < 10) {
month = "0" + month;
}
return `${dt}/${month}/${year}`;
};

const convertStatusToIcon = (status) => {
let statusIcon;

switch (status) {
case "Canceled":
return (statusIcon = <Status label="Canceled" canceled />);
case "Pending":
return (statusIcon = <Status label="Pending" pending />);
case "Deliverd":
return (statusIcon = <Status label="Deliverd" deliverd />);
default:
status = <Status label="..." />;
}

return statusIcon;
};

const convertOrdersRows = (rows) => {
let data = [...rows];

return data.map((value) => {
let convertedRow = { ...value };
convertedRow.date = convertToNormalDate(convertedRow.date);
convertedRow.status = convertStatusToIcon(convertedRow.status);
convertedRow.price = `$ ${convertedRow.price}`;
return convertedRow;
});
};

const DataGrid = (props) => {
const classes = useStyles();
const { orders, products, data } = props;
const columns = orders ? orderColumns : products ? productColumns : [];
const rows = convertOrdersRows(data);
console.log(rows);
return (
<MuiDataGrid
rows={rows}
columns={columns}
checkboxSelection
autoPageSize
{...props}
/>
);
};

export default DataGrid;

最佳答案

删除 convertOrdersRows 并使用 renderCell 属性,因为它可以访问列中的所有单元格抛出参数,以便您可以放置​​您想要的任何逻辑。

const orderColumns = [
{ field: "id", headerName: "ID", width: 120 },
{
field: "date",
headerName: "Date",
width: 140,
renderCell: (params) => convertToNormalDate(params.value),
},
{ field: "customerName", headerName: "Customer Name", width: 190 },
{ field: "products", headerName: "Products", width: 150 },
{
field: "price",
headerName: "Price",
width: 100,
renderCell: (params) => `$ ${params.value}`,
},
{
field: "status",
headerName: "Status",
width: 120,
renderCell: (params) => convertStatusToIcon(params.value),
},
];

关于reactjs - 我需要在 Material-ui DataGrid 中渲染 JSX 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67325754/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com