gpt4 book ai didi

javascript - Material-UI DataGrid 组件使用新状态数据刷新

转载 作者:行者123 更新时间:2023-12-05 06:52:53 24 4
gpt4 key购买 nike

早上好

我们正在使用 Material-UI 创建一个包含用户的 DataGrid 表,并希望能够使用按钮删除选定的条目(复选框)。

单击该按钮会更新我们对象的状态(删除预期的行),Material-UI 的 DataGrid 元素将作为参数 this.state.rows(如 this Stack Overflow 中所述) ).

下面显示的日志证明状态元素确实在 setState() 调用(蓝色矩形)之后更新,该调用重新呈现 DataGrid 组件(红色矩形)。然而,视觉上没有任何变化:(

Console Log

代码如下:

import { DataGrid } from '@material-ui/data-grid';
import * as React from 'react';

import Button from '@material-ui/core/Button';
import { makeStyles } from '@material-ui/core/styles';
import DeleteIcon from '@material-ui/icons/Delete';

class EmployeeGrid extends React.Component {

constructor(props) {
super(props);
this.handleDelete = this.handleDelete.bind(this);
this.hrefs = {}
this.columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'firstName', headerName: 'First name', width: 130 },
{ field: 'lastName', headerName: 'Last name', width: 130 },
{ field: 'description', headerName: 'Description', width: 200 }
];

let employeeList = [];
let id = 1;
for (const employee of this.props.employees) {
this.hrefs[id] = employee.url;
employeeList.push({
id: id,
firstName: employee.entity.firstName,
lastName: employee.entity.lastName,
description: employee.entity.description
});
id++;
}
console.log(this.hrefs)
console.log(employeeList)
this.state={rows: employeeList, //populate initial state with all employees from database
selected: [],
nbRender: 1}
console.log(this.state.rows);
}

handleDelete() {
for (let id of this.state.selected) {
this.state.rows.splice(id - 1, 1); //delete rows that were selected
this.props.onDelete(this.hrefs[id]); //delete from database
}
this.setState({rows: this.state.rows, selected: [], nbRender: 2}, () => {
console.log(this.state.rows) //checks state is indeed updated
});
console.log(this.state.rows);
}

render() {
console.log("In render of EmployeeGrid");
console.log(this.state.rows)
return (
<div>
<div style={{ height: 400, width: '100%' }}>
<DataGrid rows={this.state.rows} //populate grid with state.rows
columns={this.columns}
pageSize={this.props.pageSize}
checkboxSelection
onSelectionChange={(newSelection) => {
this.setState({selected: newSelection.rowIds})
}}/>
</div>
<div>
<Button variant="contained"
color="secondary"
startIcon={<DeleteIcon />}
onClick={this.handleDelete}>
Delete
</Button>
</div>
</div>
)
}
}

export default EmployeeGrid;

编辑 1

经过更多的分析,我们发现日志在控制台日志中显示4行的原因是因为我们更新了state inplace at line:

this.state.rows.splice(id - 1, 1); //delete rows that were selected

我们现在注意到,下面这行永远不会被调用,这反过来又不会重新呈现页面更新。

 this.setState({rows: this.state.rows, selected: [], nbRender: 2}, () => {
console.log(this.state.rows) //checks state is indeed updated
});

再次感谢任何关于为什么会出现这种情况的想法!

干杯,注意安全

最佳答案

好像你在这里改变了状态

this.state.rows.splice(id - 1, 1)

试试这个

handleDelete() {
for (let id of this.state.selected) {
this.props.onDelete(this.hrefs[id]); //delete from database
}
this.setState((prevState) => ({
...prevState,
rows: prevState.rows.filter((row) => !prevState.selected.includes(row.id)),
selected: [],
nbRender: 2
}));
}

关于javascript - Material-UI DataGrid 组件使用新状态数据刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65883631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com