search

Home  >  Q&A  >  body text

How to add a controllable checkbox to each row: Implementation method in MUI Datagrid

I know the way to add a checkbox or text field is to use renderCell and it works, I can see the checkbox:

However, I don't understand how I should control each row's checkbox/textfield individually. For example, what if I want row 1 to have the "fill" variant of the TextField, and row 2 to have the "outline" variant?

import * as React from "react";
import {DataGrid} from "@mui/x-data-grid";
import {Box, Checkbox, TextField} from "@mui/material";

const columns = [
  {field: "id", headerName: "ID", width: 30},
  {field: "col1", headerName: "Column 1", width: 150},
  {field: "col2", headerName: "Column 2", width: 150},
  {field: "col3", headerName: "Column 3", width: 150, renderCell: (params) => <Checkbox />},
];

const rows = [
  {id: 1, col1: "Example", col2: "Content", col3: ??????},
  {id: 2, col1: "Example", col2: "Content", col3: ??????},
  {id: 3, col1: "Example", col2: "Content", col3: ??????},
];

export default function Table() {
  return (
    <Box sx={{}}>
      <DataGrid rows={rows} columns={columns} />
    </Box>
  );
}

I tried adding a new <Checkbox /> with props like <Checkbox defaultChecked/> but of course, that didn't work of.

P粉329425839P粉329425839283 days ago500

reply all(1)I'll reply

  • P粉402806175

    P粉4028061752024-03-30 00:33:52

    Please take a look at the example I provided for you. Hope I can answer your question. https://codesandbox.io/s/optimistic-leaf-xm32lk ?file=/Demo.tsx

    import * as React from "react";
    import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid";
    import Checkbox from "@mui/joy/Checkbox";
    
    function RenderCheckBox(props: GridRenderCellParams<any, boolean>) {
      const [checked, setChecked] = React.useState(props.value); // Initiated react binded value with param from `rows`
    
      // Handler for user clicks to set checkbox mark or unset it
      const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        setChecked(event.target.checked);
      };
      //The bind for dynamic mark/unmark: checked={checked}
      //The handler for user clicks: onChange={handleChange}
      return (
        <Checkbox
          label="some text"
          size="lg"
          checked={checked} 
          onChange={handleChange} 
        />
      );
    }
    
    const columns: GridColDef[] = [
      { field: "id", headerName: "ID", width: 30 },
      { field: "col1", headerName: "Column 1", width: 150 },
      { field: "col2", headerName: "Column 2", width: 150 },
      {
        field: "checked",
        headerName: "Column 3",
        width: 150,
        renderCell: RenderCheckBox
      }
    ];
    // Here 'checked' field will pass the param to component.
    const rows = [
      { id: 1, col1: "Example", col2: "Content", checked: true },
      { id: 2, col1: "Example", col2: "Content", checked: false },
      { id: 3, col1: "Example", col2: "Content", checked: false }
    ];
    
    export default function RenderCellGrid() {
      return (
        <div style={{ height: 300, width: "100%" }}>
          <DataGrid rows={rows} columns={columns} />
        </div>
      );
    }

    reply
    0
  • Cancelreply