首页 > 解决方案 > 插入多行物料表

问题描述

我正在尝试在 mat 表中添加多行,因此我有一个字段,用户可以在其中引入数字,例如:“2”

然后我问他是想将它作为一行引入还是单独引入,如果一切都在一行中,它很好但是当我尝试单独添加它时它会中断并且只引入一行而不是写的数字

然后循环for并setData()在for内部

const [data, setData] = useState([]);
const json = {
    id: id,
    name: null,
};

const newArr3 = [];
for (let index = 0; index < number; index++) {
    newArr3.push(json);
    postRequest(json);
    setData(newArr3);
}

<MaterialTable
    columns={columns}
    data={data}
    title="Table 2"
    options={{
        exportButton: true,
        filtering: true,
        actionsColumnIndex: -1,
        headerStyle: {
            backgroundColor: '#01579b',
            color: '#FFF'
        },
        rowStyle: {
            backgroundColor: '#EEEEEE',
        }
    }}
/>

我从组件反应工具开发人员那里看到它在数组中引入了 2 个对象,data但只显示了一个,可能是因为它本来应该是相同的:tabledata {id:0}但我不知道该怎么做。tabledata {id:0}tabledata {id:1}

提前致谢

标签: javascriptreactjsmaterial-table

解决方案


尝试 ty give indexof forloop as id

const newArr3 = [];
for (let index = 0; index < number; index++) {
    const json = {
        id: index,
        name: null
    }
    newArr3.push(json);
    postRequest(json);
    setData(newArr3);
}

推荐阅读