我正在尝试创建一种基于JSON响应动态映射字段的表单。
我目前有一个虚拟对象,我将其设置为状态,但是最后阶段将接收来自外部API调用的数据。数据集可能会随时间变化,并根据业务需求添加或删除键/值。作为替代,我需要创建一个智能组件,该组件需要一组初始数据并为对象中的每个键/值对映射一个“只读”字段。
第二个问题是实际的表单布局。我在下面有一个初始脚手架,并且只对列和行进行了硬编码。如何实现从数据响应中创建两列行的逻辑?
对此有任何想法/帮助,深表感谢!
代码:
import React, { Component } from 'react';
import { render } from 'react-dom';
import { Form, Button, Input, Row, Col } from 'antd';
import "antd/dist/antd.css";
import './style.css';
class OpportunityDetails extends Component {
constructor() {
super();
this.state = {
disabled: true,
formLayout: 'horizontal',
opportunityDetails: [
{
CustomerName: "",
Field2: "Some data",
Field3: "Some data",
Field4: "Some data",
Field5: "Some data",
Field6: "Some data",
Field7: "Some data",
Field8: "Some data",
Field9: "Some data",
Field10: "Some data",
Field11: "Some data",
Field12: "Some data",
Field13: "Some data",
Field14: "Some data",
Field15: "Some data"
}
]
};
this.toggleSwitch = this.toggleSwitch.bind(this)
}
toggleSwitch() {
this.setState(previousState => ({
disabled: !previousState.disabled,
enabled: previousState.disabled
}))
}
modifyRoute(){
alert("Sending you to the modify floor");
}
uploadRoute(){
alert("Sending you to the upload sector!")
}
render() {
const { disabled } = this.state;
const { enabled } = this.state;
return (
<div className={}>
/// Button Group
<Row type="flex" justify="space-around">
<Col span={4}>
<Button disabled={disabled} onClick={this.modifyRoute}>Modify Docs</Button>
</Col>
<Col span={4}>
<Button disabled={disabled} onClick={this.uploadRoute}>Upload Docs</Button>
</Col>
<Col span={4}>
<Button disabled={enabled} onClick={this.toggleSwitch}>
Unlock Quote
</Button>
</Col>
</Row>
/// Form section with columns and rows for all key/value pairs
<Row type="flex" justify="space-around">
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
<Col span={10}>
<Form.Item label={key}>
<Input placeholder="{value} />
</Form.Item>
</Col>
</Row>
<Row type="flex" justify="space-around">
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
</Row>
<Row type="flex" justify="space-around">
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
</Row>
<Row type="flex" justify="space-around">
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
<Col span={10}>
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
</Col>
</Row>
</div>
);
}
}
render(<OpportunityDetails />, document.getElementById('root'));
答案 0 :(得分:1)
认真对待阿米尔的评论。然后,您可能会重组数据,这样会更方便(更易读):
我不知道为什么opportunityDetails
必须是数组,无论如何我都对此进行了调整。尽管它会增加您所看到的复杂性
1。)这就是您希望数据看起来像的样子(稍后您将了解原因):
/*[
{ itemKey: "CustomerName", itemValue: "" },
{ itemKey: "Field2", itemValue: "Some data" }
.... and so on
]*/
opportunityDetails: [
{
CustomerName: "",
Field2: "Some data",
Field3: "Some data",
Field4: "Some data",
Field5: "Some data",
Field6: "Some data",
Field7: "Some data",
Field8: "Some data",
Field9: "Some data",
Field10: "Some data",
Field11: "Some data",
Field12: "Some data",
Field13: "Some data",
Field14: "Some data",
Field15: "Some data"
}
].map(obj => {
const objKeys = Object.keys(obj);
return objKeys.map(itemKey => {
return {
itemKey,
itemValue: obj[itemKey]
};
});
})
以下以下步骤和模式是JSX(或反应)特定的;我要说的是,这是在反应中呈现array of JSX elements
的最常见方式:
2。)创建一个将返回 JSX数组的方法,我使用了.map,因为.forEach无法正常工作(您可能要自己搜索原因)。
renderDynamicElWrapper() {
return this.state.opportunityDetails.map(items => {
return (
<Row type="flex" justify="space-around">
{this.renderDynamicEl(items)}
</Row>
);
});
}
假设机会详细信息将包含1个以上的项目,我们需要采用与上述方法类似的另一种方法和迭代方法
renderDynamicEl(els) {
return els.map(el => {
return (
<Col span={10}>
<Form.Item label={el.itemKey}>
<Input placeholder={el.itemValue} />
</Form.Item>
</Col>
);
});
}
3。)最后,下面是您的render
方法的返回结果:
return (
<div>
<Row type="flex" justify="space-around">
<Col span={4}>
<Button disabled={disabled} onClick={this.modifyRoute}>
Modify Docs
</Button>
</Col>
<Col span={4}>
<Button disabled={disabled} onClick={this.uploadRoute}>
Upload Docs
</Button>
</Col>
<Col span={4}>
<Button disabled={enabled} onClick={this.toggleSwitch}>
Unlock Quote
</Button>
</Col>
</Row>
{this.renderDynamicElWrapper()}
</div>
);
}
P.S:我建议您熟练掌握.map
以及如何从方法返回jsx数组,因为%100的情况下您将在React项目中遇到相同的模式。
答案 1 :(得分:0)
<Row type="flex" justify="space-around">
<Col span={10}>
{opportunityDetails.map(detail=>{
return detail.forEach(([key,value])=>(
<Form.Item label={key}>
<Input placeholder={value} />
</Form.Item>
)
)
})}
</Col>
</Row>