I am using @material-ui/data-grid to display some data and each row must have a link to the next page. I can pass all the required data, but am not sure how to create a link. The documentation doesn't mention it anywhere. I tried to pass it with the valueGetter like in the example below but due to how React renders HTML it just returns the href as a string.
const columns = [
{
field: "id",
headerName: "ID",
width: 150,
valueGetter: (params) =>
`<a href="${params.getValue("id")}">${params.getValue("id")}</a>`,
},
{ field: "inviteId", headerName: "Invite Id", width: 150 },
];
What you need to do is include a renderCell method in your columns array. In the above I am rendering a Button inside columns 5 and 6 which will appear on every populated row. Above that you can have a function which creates and returns a Button from Material-ui. This should be the default answer.
A particularly useful feature is the renderCell prop, which is a prop of the columns object in the DataGrid. The renderCell prop allows you to inject components into the DataGrid, creating a column of custom cells. This post will explore the code for creating Buttons and Links in the DataGrid.
Material UI's Data Grid is a powerful and flexible data table. It makes it easy for you to display data and perform out of the box functionalities such as editing, sorting, filtering, pagination and more. In this article, we'll discuss why you should use the Data Grid in Material UI in your project.
Adding a link to the DataGrid is similar to adding a Button. Below is my Column object: This column of the DataGrid will simply contain a Material-UI Link component. I am accessing cellValues.row.url to set the routing destination when the Link component is clicked.
There are some important reasons you might want to use the Material UI Data Grid: The Data Grid offers accessibility features such as cell highlighting. That is, every cell is accessible using the keyboard.
Material-UI’s DataGrid can be customized to meet a variety of use cases. A particularly useful feature is the renderCell prop, which is a prop of the columns object in the DataGrid. The renderCell prop allows you to inject components into the DataGrid, creating a column of custom cells.
In terms of interaction, the Data Grid provides an inbuilt feature such as row selection by default. This allows the user to select certain rows on mouse click or using certain keyboard shortcuts.
You're returning the link as a string:
`<a href="${params.getValue("id")}">${params.getValue("id")}</a>`
Could you not return this as JSX?
return (<a href={params.getValue("id")}>{params.getValue("id")}</a>)
The above is not correct as valueGetter
is going to return a string.
It appears you will need to switch out valueGetter
with renderCell
, which allows you to render a React node. The Material UI docs provide an example here.
Updated.
const columns = [
{
field: "id",
headerName: "ID",
width: 150,
renderCell: (params) =>
<a href="${params.row.id}">${params.row.id}</a>,
},
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With