Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a link in a row with @material-ui/data-grid

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 },
];
like image 711
Ales Maticic Avatar asked Nov 12 '20 22:11

Ales Maticic


People also ask

How do you add a button to every row in a material ui table?

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.

What is renderCell?

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.

What is data grid in material ui?

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.

How do I add a link to the DataGrid?

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.

Why should I use the material UI data grid?

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.

How do I use the rendercell prop in material-UI DataGrid?

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.

How do I use the data grid?

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.


Video Answer


2 Answers

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>)

Update

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.

like image 99
Josh Avatar answered Oct 08 '22 22:10

Josh


Updated.

const columns = [
  {
    field: "id",
    headerName: "ID",
    width: 150,
    renderCell: (params) => 
      <a href="${params.row.id}">${params.row.id}</a>,
  },
];
like image 2
acxes Avatar answered Oct 08 '22 20:10

acxes