【发布时间】:2018-10-28 12:53:08
【问题描述】:
我使用了简单的表格(代码附加在下面)并尝试查找许多参考资料,但找不到适合我的问题的解决方案。该表显示正常,但是当添加、更新或删除任何数据时问题就开始了。除非刷新页面,否则它不会被反映。我尝试过的几种方法是更新状态但无济于事。我什至尝试过强制更新组件但徒劳无功。我什至尝试渲染组件。
<div className="table">
<table className="table table-striped table-padding">
<thead>
<tr className="heading">
<th className="col-md-2">Image</th>
<th className="col-md-3">Office Name</th>
<th className="col-md-5">Address</th>
<th className="col-md-1"></th>
<th className="col-md-1"></th>
</tr>
</thead>
<tbody>
{
(this.state.dataUpdated) &&
this.state.data.map((list, index) => {
return (
<tr key={index}>
<td><img style={{height: '30px', width: '30px'}} src={list.imageData} alt="icon"/></td>
<td>{list.name}</td>
<td>{list.address}</td>
<td><button type="button" onClick={(e) => this.handleEdit(list.officeId)}><i className="fa fa-pencil" aria-hidden="true"></i></button></td>
<td><button type="button" onClick={(e) => this.handleDelete(list.officeId)}><i className="fa fa-trash" aria-hidden="true"></i></button></td>
</tr>
)
})
}
</tbody>
</table>
</div>
handleEdit(id) {
this.state.data.map(row => {
if(row.officeId === id) {
return (
this.setState({ displayData: row })
)
}
});
this.displayOfficeList();
}
handleDelete(id) {
const { dispatch } = this.props;
dispatch(commonActions.deleteOffice(id));
this.displayOfficeList();
}
displayOfficeList() {
this.toggleFlag();
commonServices.getOfficeList().then((res) => {
let demo = res;
this.setState({ data: demo.content});
this.setState({ dataUpdated: true});
});
}
【问题讨论】:
-
请添加handleEdit、handleDelete功能代码
-
向我们展示您如何处理数据的删除和添加。
-
@Dieshundefined 代码已更新。
-
@null 代码已更新。请检查。
标签: reactjs html-table crud page-refresh rerender