reactjs - 如何在设置值时更改 React 中的输入值并在状态是数组对象时更新状态?
问题描述
这是我的代码。
class Sample extends Component {
constructor(props) {
super(props);
this.state = {
editData: null,
editModalOpen: false,
obj: {
"content": [{
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1001,
"dataFileContainerDtoList": [{
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1053,
"name": "Sheet1",
"headerDtoList": [{
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1109,
"name": "Fname",
"indexedName": "Fname",
"candidateName": null,
"custom": false
},
{
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1111,
"name": "license number",
"indexedName": "license number",
"candidateName": null,
"custom": false
}]
}, {
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1054,
"name": "Sheet2",
"headerDtoList": [{
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1113,
"name": "Fname",
"indexedName": "Fname",
"candidateName": null,
"custom": false
},{
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1115,
"name": "license number",
"indexedName": "license number",
"candidateName": null,
"custom": false
}, {
"updateOn": null,
"createdOn": {
"year": 1399,
"month": 8,
"day": 8
},
"updatedBy": null,
"createdBy": "admin",
"id": 1116,
"name": "desc",
"indexedName": "desc",
"candidateName": null,
"custom": false
}]
}]
}],
"first": true,
"numberOfElements": 13,
"size": 1000,
"number": 0
}
}
}
editFileData = (editData) => {
this.editModalOpen();
this.setState({
editData: editData
})
}
editFileDataHeaderChange = (i, j) => {
const { editData } = this.state;
this.setState({
editData: editData // How change the state of editData
})
}
editModalOpen = () => {
this.setState({
editModalOpen: true
})
}
handleEditTabChange = (event, value) => {
this.setState({
editIndexValue: value
})
}
render() {
return (
<div>
<TableBody>
{this.state.obj != null && this.state.obj.map((item) => (
<StyledTableRow key={item.id}>
<StyledTableCell align="right">
<EditIcon onClick={() => this.editFileData(item)}/>
</StyledTableCell>
</StyledTableRow>
))}
</TableBody>
<Dialog onClose={this.editFileModalClose} open={editModalOpen}>
<DialogContent dividers>
<form enctype="multipart/form-data">
<Grid container spacing={3}>
{editData !== null &&
[<Grid item xs={12} sm={12} md={12} lg={12}>
<div>
<AppBar position="static" color="default">
<Tabs
value={editIndexValue}
onChange={this.handleEditTabChange}
>
{editData.dataFileContainerDtoList.map((item, i) =>
<Tab label={editData.dataFileContainerDtoList[i].name} id={'full-width-tab-' + i} aria-controls={'full-width-tabpanel-' + i} />
)}
</Tabs>
</AppBar>
<SwipeableViews
index={editIndexValue}
onChangeIndex={this.handleChangeIndex}
>
{editData.dataFileContainerDtoList.map((item, i) =>
<TabPanel value={editIndexValue} index={i}>
{item.headerDtoList.map((item, j) =>
<TextField
onChange={this.editFileDataHeaderChange}
value={editData.dataFileContainerDtoList[i].headerDtoList[j].name}
type="text"
/>
)}
</TabPanel>
)}
</SwipeableViews>
</div>
</Grid>,
<Grid item xs={12} sm={12} md={12} lg={12} className={classNames(classes.textCenter)}>
<Button
variant="contained"
color="primary"
size="large"
onClick={this.editSubmitFileData}>
Submit
</Button>
</Grid>]}
</Grid>
</form>
</DialogContent>
</Dialog>
</div>
)
}
}
我有带有“内容”键的 obj,它是许多对象的数组。我"content.dataFileContainerDtoList[i].headerDtoList[i].name"
以模态显示并需要对其进行编辑。但是由于我设置的值,无法以模式更改 Textfield 中的值并设置新状态。如何使输入的值更改并更新对象状态的一部分?
解决方案
推荐阅读
- c# - 对 DataGridCell 应用填充后,DataGridCell 在 WPF 的顶部和底部显示空格
- excel - 创建Excel函数以获取雅虎财经股票报价
- javascript - Need help to induction curl Post in code JS / Node.js
- ldap - LDAP - Difference between apt-get and build from sources installation
- php - PHP联系表格多个复选框发送电子邮件
- angular - ionic 3 multi level search filter
- react-native - 反应原生 redux mapStateToProps 状态更新但不重新渲染演示者组件
- c++ - 放置删除函数永远不会在异常时调用
- php - PHP & SQL LEFT JOIN when i echo it display only content with id 1
- ios - 尤里卡表单与自定义表格视图连接?