Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add MUI DataGrid Edit/delete Button

I am having a hard time adding custom buttons per row in the DataGrid component.

This is my code:

const columns = [
    { field: 'model_id', headerName: 'ID', width: 70, sortable: false },
    { field: 'model_code', headerName: 'Model Code', width: 130 },
    { field: 'model_description', headerName: 'Description', width: 400 },
    { field: 'actions', headerName: 'Actions', width: 400 }
];

DataGrid:

<DataGrid
rows={models}
columns={columns}
getRowId={(row) => row.model_id}
pageSize={5}
rowsPerPageOptions={[5]}
checkboxSelection />

What I want per row: enter image description here

like image 399
Pusoy Avatar asked Jan 30 '26 04:01

Pusoy


1 Answers

You can use renderCell function in column definition array like this:

const onButtonClick = (e, row) => {
    e.stopPropagation();
    //do whatever you want with the row
};

const columns = [
    { field: 'model_id', headerName: 'ID', width: 70, sortable: false },
    { field: 'model_code', headerName: 'Model Code', width: 130 },
    { field: 'model_description', headerName: 'Description', width: 400 },
    { field: 'actions', headerName: 'Actions', width: 400, renderCell: (params) => {
        return (
          <Button
            onClick={(e) => onButtonClick(e, params.row)}
            variant="contained"
          >
            Delete
          </Button>
        );
      } }
];

You can take a look at this stackblitz for a live working example of this solution.

like image 127
Ahmet Emre Kılınç Avatar answered Feb 02 '26 00:02

Ahmet Emre Kılınç



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!