我试图在Material-UI的datagrid组件中的一个单元格中有两行单独的文本,其中一行文本位于另一行的顶部,似乎什么也无法工作。这是我尝试过的:
const columns: ColDef[] = [
{
field: "name",
headerName: "Sector",
width: 300,
renderCell: (params) => {
let name = params.data.name
const flow = params.data as IOFlow;
const title = Currency.format(flow.value)
+ " " + props.direction
+ " per " + Currency.format(1);
return (
<>
<Typography fullWidth display='block'>
{name}
</Typography>
<br/>
<Typography fullWidth display='block'>
{title}
</Typography>
</>
);
}
},
];
请让我知道是否还有我应该提供的信息。
主要问题是您不应<>...</>
在单元格内容周围使用片段()。使用一个<div>
代替。单元格的样式display: flex
将水平排列直接子级(即您的Typography元素)。如果将单元格内容包装在中<div>
,内的内容<div>
将按预期运行。
这是一个工作示例:
import * as React from "react";
import { DataGrid } from "@material-ui/data-grid";
import Typography from "@material-ui/core/Typography";
const columns = [
{
field: "name",
headerName: "Sector",
width: 300,
renderCell: (params) => (
<div>
<Typography>{params.value.name}</Typography>
<Typography color="textSecondary">{params.value.title}</Typography>
</div>
)
}
];
const rows = [
{
id: 1,
name: { name: "Name1", title: "Title1" }
},
{
id: 2,
name: { name: "Name2", title: "Title2" }
},
{
id: 3,
name: { name: "Name3", title: "Title3" }
}
];
export default function RenderCellGrid() {
return (
<div style={{ height: 300, width: "300" }}>
<DataGrid rows={rows} columns={columns} />
</div>
);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句