Search code examples
javascriptreactjsmaterial-uimui-datatable

React MUI-Datatables get row id


I have a table that renders two buttons, delete and edit row.

On both of them I need to access the row Id.

I tried to use customBodyRender but it did not work, i have only the dataIndex and the rowIndex, but what I need is the actual row object value.

Updated question with the code

const columns = [
{
  name: "id",
  label: "Id",
  options: {
    display: false
  }
},
{
  name: "name",
  label: "Name",
},
{
  name: "Actions",
  options: {
    filter: false,
    sort: false,
    empty: true,
    customBodyRender: (dataIndex, rowIndex) => {
      return (
        <>
          <IconButton aria-label="edit" onClick={() => {
            alert(dataIndex + " - " + rowIndex)
          }}>
            <EditIcon />
          </IconButton>
          <IconButton color="primary" aria-label="delete" style={{ marginLeft: "10px" }} onClick={() => {
            alert(dataIndex)
          }}>
            <DeleteIcon />
          </IconButton>
        </>
      );
    }
  }
}];

This is how MUIDataTable is being used

<MUIDataTable
      title={"Lista de Turnos"}
      data={shifts}
      columns={columns}
      options={{
        selectableRowsHideCheckboxes: true,
        textLabels: {
          body: {
            noMatch: 'Não foram encontrados registros para serem mostrados',
          },
        },
      }}
    />

Solution

  • You can use customBodyRenderLite instead of customBodyRender

    The actual code would be like this if you want to access the actual data object.

    import React from "react";
    import ReactDOM from "react-dom";
    import MUIDataTable from "mui-datatables";
    import Button from '@material-ui/core/Button'
    
    function App() {
    
      const data = [
        {id:1,name:'wahid'},
        {id:2,name:'jamil'},
        {id:3,name:'marin'},
      ];
    
      const columns = [
        {
          name: "id",
          label: "Id",
          options: {
            display: false
          }
        },
        {
          name: "name",
          label: "Name",
        },
        {
          name: "Actions",
          options: {
            filter: false,
            sort: false,
            customBodyRenderLite: (dataIndex, rowIndex) => {
              return (
                  <Button aria-label="edit" onClick={() => {
                    alert(data[dataIndex].name)
                  }}>
                    Button
                  </Button>
                  
              );
           }
        },
        
      }
      ];
    
    
      return (
        <React.Fragment>
          <MUIDataTable
            title={"ACME Employee list"}
            data={data}
            columns={columns}
              options={{
                selectableRowsHideCheckboxes: true,
                textLabels: {
                  body: {
                    noMatch: 'Não foram encontrados registros para serem mostrados',
                  },
                },
              }}
          />
        </React.Fragment>
      );
    }
    
    ReactDOM.render(<App />, document.getElementById("root"));