Search code examples
reactjsmaterial-table

Value is not updating in MaterialTable


I try to edit the value and update the same but its not updating in MaterialTable Below is the link which am refering https://material-table.com/#/docs/features/editable under this am refering Cell Editable Example

what am missing here

any suggestion?

please refer below snippet

import React from "react";
import MaterialTable from "material-table";

export default function CellEditable() {
  const { useState } = React;

  const [columns, setColumns] = useState([
    { title: "Name", field: "name" },
    {
      title: "Surname",
      field: "surname",
      initialEditValue: "initial edit value"
    },
    { title: "Birth Year", field: "birthYear", type: "numeric" },
    {
      title: "Birth Place",
      field: "birthCity",
      lookup: { 34: "İstanbul", 63: "Şanlıurfa" }
    }
  ]);

  const [data, setData] = useState([
    { name: "Mehmet", surname: "Baran", birthYear: 1987, birthCity: 63 },
    { name: "Zerya Betül", surname: "Baran", birthYear: 2017, birthCity: 34 }
  ]);

  return (
    <MaterialTable
      title="Cell Editable Preview"
      columns={columns}
      data={data}
      cellEditable={{
        onCellEditApproved: (newValue, oldValue, rowData, columnDef) => {
          return new Promise((resolve, reject) => {
            console.log("newValue: " + newValue);
            setTimeout(resolve, 1000);
          });
        }
      }}
    />
  );
}

Solution

  • The code you are using never sets the state, it just logs to the console.

    You need to set the state at some point. I was able to get this working, though I am uncertain if this is the correct approach.

    Update

    If you wish to disable a specific column you can add editable: 'never' to the specific column. See below where I added this to the birthYear.

    function CellEditable() {
      const { useState } = React;
    
      const [columns, setColumns] = useState([
        { title: 'Name', field: 'name' },
        { title: 'Surname', field: 'surname', initialEditValue: 'initial edit value' },
        { title: 'Birth Year', field: 'birthYear', type: 'numeric', editable: 'never' },
        {
          title: 'Birth Place',
          field: 'birthCity',
          lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
        },
      ]);
    
      const [data, setData] = useState([
        { name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
        { name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
      ]);
    
      return (
        <MaterialTable
          title="Cell Editable Preview"
          columns={columns}
          data={data}
          cellEditable={{
            onCellEditApproved: (newValue, oldValue, rowData, columnDef) => {
              return new Promise((resolve, reject) => {
                const clonedData = [ ...data ]
                clonedData[rowData.tableData.id][columnDef.field] = newValue
                setData(clonedData)
                
                setTimeout(resolve, 1000);
              });
            }
          }}
        />
      )
    }