首页 > 解决方案 > 如何在向表中添加列时动态更改列标题

问题描述

这是用于编辑表格单元格的工作代码笔: https ://codepen.io/gges5110/pen/GLPjYr?editors =0010

添加列功能如下:

handleAddColumn = () => {
    const { columns } = this.state;
    const newColumn = {
      title: 'age',
      dataIndex: 'age',
    };

我该如何做到这一点,以便每次单击“添加列”时,它都会将列标题更改为“1 岁”、“2 岁”、“3 岁”……?

标签: javascriptreactjsevent-handlingreact-table

解决方案


试试这个,我使用了你添加的相同计数状态,需要在 addColumn 上更新它。

handleAddColumn = () => {
const { columns } = this.state;
const newColumn = {
  title: `age ${this.state.count}`,
  dataIndex: 'age',
};
this.setState({
  count: this.state.count + 1,
});
this.setState({
  columns: [...columns, newColumn]
})

}


推荐阅读