Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<DataGrid> property "onCellEditCommit" doesn't work

I'm using the DataGrid component of Material-UI for displaying a list of items.

I want to make each cell in every row editable and then change the corresponding item of that row.

for that purpose, I'm using the onCellEditCommit property provided on the DataGrid component. But, when I actually change a value of a cell, my event handler doesn't invoke.

I use the following code:

 export default function TargetsView(props) {
      const [items] = useContext(TargetsContext);
      const onItemsEnabled = props.onItemsEnabled;

      return (
        <div style={{ height: 300, width: "100%" }}>
          <DataGrid
            disableSelectionOnClick
            onCellEditCommit={(params) => {
              console.log("test");
            }}
            onSelectionModelChange={
              onItemsEnabled
                ? (selectedRows) => {
                    const targetInformation = [];
                    for (let selectedRow of selectedRows)
                      targetInformation.push(items[selectedRow - 1]);
                      onItemsEnabled(targetInformation);
                  }
                : null
            }
            rows={items}
            columns={columns}
            checkboxSelection
            experimentalFeatures={{ newEditingApi: true }}
          />
        </div>
      );
    }
like image 531
Idan Krupnik Avatar asked Feb 27 '26 09:02

Idan Krupnik


1 Answers

OnCellEditCommit is part of the legacy editing api: https://mui.com/x/react-data-grid/editing-legacy/#events

Your code should not contain the line: experimentalFeatures={{ newEditingApi: true}}

 export default function TargetsView(props) {
      const [items] = useContext(TargetsContext);
      const onItemsEnabled = props.onItemsEnabled;

      return (
        <div style={{ height: 300, width: "100%" }}>
          <DataGrid
            disableSelectionOnClick
            onCellEditCommit={(params) => {
              console.log("test");
            }}
            onSelectionModelChange={
              onItemsEnabled
                ? (selectedRows) => {
                    const targetInformation = [];
                    for (let selectedRow of selectedRows)
                      targetInformation.push(items[selectedRow - 1]);
                      onItemsEnabled(targetInformation);
                  }
                : null
            }
            rows={items}
            columns={columns}
            checkboxSelection
          />
        </div>
      );
    }
like image 157
cementdriveway Avatar answered Mar 02 '26 05:03

cementdriveway



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!