【发布时间】:2020-06-14 02:51:00
【问题描述】:
我是新手,我正在尝试从 api 获取数据,然后将数据分配给 dict 常量。之后,我想将字典加载到自定义表中并显示这些数据。我想做的是做类似data.Table.FIRST_TABLE.data = response.data.data 的事情,打算将从post API 获取的data 分配给data 字典。提前感谢您帮助我。代码如下:
let data = {
Table:{
'FIRST_TABLE':{
settings: {
colOrder: ['first_col', 'second_col'],
},
columns:{
'first_col':{
visible: true,
},
'second_col':{
visible: true,
},
},
data:[],
},
},
}
class MainLayout extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit = e => {
e.preventDefault();
axios
.post('http://localhost:4000/api/loadData/')
.then(function(response){
data.Table.FIRST_TABLE.data = response.data.data //stick data to data dict
})
.catch(err => {
console.error(err);
});
};
render() {
return (
<form onSubmit={this.handleSubmit}>
<input
type="submit"
value="Check Mapping"
onClick={this.onSubmit}
/>
<CustomTable state={data}>
//data dict define the table structure and data presenting
<Table />
</CustomTable>
</form>
);
}
}
【问题讨论】:
标签: javascript reactjs api axios state