最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Material UI: Can I use React component in the DataGrid cell instead of predefined cell types ('string&#

programmeradmin0浏览0评论

React, Material UI, TypeScript
I want to put React component into the DataGrid cell. I read about cell custom types here but it didn't help me: it allows to manage by string format only, but I need to use component instead of string. For example I want to use Link component in the cell. My code:

{ field: 'name', headerName: 'Name', width: 200,
    valueGetter: (params: ValueGetterParams): JSX.Element =>
        (<Link href={this.props.createRecordUrl(params.row.logicalName as EntityNames,
            params.row.id as ID)} target="_blank">{params.row.name}</Link>)
},

But I get the [object Object] string instead of Link component in the cell. How can I solve the problem?

React, Material UI, TypeScript
I want to put React component into the DataGrid cell. I read about cell custom types here but it didn't help me: it allows to manage by string format only, but I need to use component instead of string. For example I want to use Link component in the cell. My code:

{ field: 'name', headerName: 'Name', width: 200,
    valueGetter: (params: ValueGetterParams): JSX.Element =>
        (<Link href={this.props.createRecordUrl(params.row.logicalName as EntityNames,
            params.row.id as ID)} target="_blank">{params.row.name}</Link>)
},

But I get the [object Object] string instead of Link component in the cell. How can I solve the problem?

Share Improve this question edited Dec 9, 2020 at 16:53 Andrey Bushman asked Dec 9, 2020 at 16:44 Andrey BushmanAndrey Bushman 12.5k23 gold badges102 silver badges210 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

You can insert custom components using the renderCell property, which you define in the columns. Have a look at this link for more information: https://material-ui.com/components/data-grid/columns/

Example taken from Material UI Docs:

const columns: GridColDef[] = [
  {
    field: 'date',
    headerName: 'Year',
    renderCell: (params: GridCellParams) => (
      <strong>
        {(params.value as Date).getFullYear()}
        <Button
          variant="contained"
          color="primary"
          size="small"
          style={{ marginLeft: 16 }}
        >
          Open
        </Button>
      </strong>
    ),
  },
];

It looks like using components in columns is only part of the paid XGrid feature? I've used material-table in the past, which does allow using custom components for columns.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论