2

我已经将 Material UI 中的材料表用于 React。当数据静态传递时,它会逐行显示,但是当我从 Web 服务中获取数据时,已经添加了行更新而不是追加新行。请帮忙。

const [state, setState] = useState({
columns: [
  { title: 'S.No', field: 'Id', type: 'numeric', editable: 'never' },
  { title: 'Roll Number', field: 'Studentid' },
  { title: 'Name', field: 'Name', editable: 'never' },
  { title: 'Year', field: 'Year', editable: 'never' },
  { title: 'Department', field: 'DeptName', editable: 'never'},
],
data: eventRegistrationStudentsList,
});

  <MaterialTable
    style={{ maxWidth: "95%" }}
    title="Student Details"
    id="studentDetailsTable"
    columns={state.columns}
    data={eventRegistrationStudentsList}
    editable={{
      onRowAdd: newData =>
        new Promise(resolve => {
              const data = [...state.data];
              data.push(newData);
              setState({ ...state, data });
              handleStudentIdChange(newData.Studentid);
              resolve();
        }),
      onRowDelete: oldData =>
        new Promise(resolve => {
          setTimeout(() => {
            resolve();
            const data = [...state.data];
            data.splice(data.indexOf(oldData), 1);
            setState({ ...state, data });
          }, 600);
        }),
    }}
    options={{
      search: false,
      actionsColumnIndex: -1
    }}
  />

eventRegistrationStudentsList 持有:

[ DeptName: "AERONAUTICAL ENGINEERING",
Id: 1,
Name: "Jon",
Studentid: "190AE101",
Year: "III" ]
4

1 回答 1

0

问题出在这里

data={eventRegistrationStudentsList}

您正在更新该州的列表,但不使用该州的列表

data={state.data}

这里。

于 2019-09-09T15:12:17.950 回答