通过单击按钮向反应 table 添加一行

adding a row to the react table by clicking a button

我想在现有的 table 中添加一个新行。有什么方法可以实现我已经阅读了 reactdatagrid 的 api 但没有找到它。还有其他方法吗?

只需为您在 table 上提供的数据添加一个新条目。所以当你按下按钮追加数据时设置状态。

首先,您需要将需要在 table 中呈现的数据映射到您的组件状态。让我们说一个数组。

tableData = [
  {
    id: 1,
    name: 'Tim',
    age: 25,
  },
  {
    id: 2,
    name: 'Jane',
    age: 22,
  },
]

并用该状态数组填充 table。然后添加一个按钮,然后单击按钮,将一些空值推送到数组。

const { tableData } = this.state;
tableData.push({ id: null, name: '', age: null});
this.setState({ tableData });