代码框链接:https://codesandbox.io/s/compassionate-sanderson-1m8nv?file=/src/App.js
我找不到关于这个题目的很多资料。以下是我希望达到的目标:
我希望用户能够编辑数据库中已经存在的采购订单的一些细节,然后用表单重新提交采购订单。原始采购订单的详细信息应显示在表单输入字段中,用户可以通过这些字段直接更改订单,然后提交。
我对网络开发不了解,所以请容忍我。
我希望最终表单对象如下所示:
{
po_number:"123abc",
carrier:"Fastway",
items: [{
item_code:"dnh75n",
quantity:"10",
special_requirements:"Add picture of happy dog"
},
{
item_code:"456def",
quantity:"4",
special_requirements:"Do not include lids"
}
]
}生成的表单输入字段的数量将基于采购订单中的项目数。下面我创建了一个简单的React组件来演示我想要做的事情。或者只是签出上面的代码沙箱链接。任何帮助都将不胜感激。我甚至找不到关于如何分组ANTd表单项以在采购订单中创建项目数组的信息。我在他们的网站上看到了很多动态表单示例,但是我想要创建基于购买订单中已有项目的表单,而不是添加用户输入的字段。
import { Form, Input, Button } from 'antd';
//This is the order that already exists
const order = {
po_number:"123abc",
carrier:"Fastway",
items: [{
item_code:"dnh75n",
quantity:"10",
special_requirements:"Add picture of happy dog"
},
{
item_code:"456def",
quantity:"4",
special_requirements:"Do not include lids"
}
]
};
const GroupForm = () => {
const onFinish = values => {
console.log(values);
}
//Create form fields based off how many items are in the order
const itemInputs = order.items.map(item => {
return (
<div>
<b>Item{" " + item.item_code}</b>
<Form.Item name={item.item_code + "_quantity"} label="quantity">
<Input defaultValue={item.quantity} style={{width: "500px"}} />
</Form.Item>
<Form.Item name={item.item_code + "_requirements"} label="speacial requirements">
<Input defaultValue={item.special_requirements} style={{width: "500px"}} />
</Form.Item>
</div>
);
});
return(
<div>
<Form onFinish={onFinish}>
<b>{"Order " + order.po_number}</b>
<Form.Item name="carrier" label="carrier">
<Input defaultValue={order.carrier} style={{width: "500px"}} />
</Form.Item>
<b>Order Items</b>
{itemInputs}
<Form.Item>
<Button type="primary" htmlType="submit"> Change Details </Button>
</Form.Item>
</Form>
</div>
);
}
export default GroupForm;发布于 2022-03-12 09:26:37
感谢Scratch‘N’put,我使用您的解决方案进一步将字段放入antd表中。
首先,一个简短的说明:以前的解决方案中的fieldKey={[field.fieldKey, "quantity"]}并不适用于我,但是将它更改为key={[field.key, "quantity"]}确实有好处。
//define the columns for the table
const columns = [
{
title: "ITEM#",
dataIndex: "item_code",
key: "item_code",
width: "12%",
//use the field here to get all infos for the form
render: (_, field) => (
<Form.Item
{...field}
name={[field.name, "item_code"]}
key={[field.key, "item_code"]}
noStyle
>
<Input placeholder="ITEM#" />
</Form.Item>
),
},
{
title: "Quantity",
dataIndex: "quantity",
key: "quantity",
render: (_, field) => (
<Form.Item
{...field}
name={[field.name, "quantity"]}
//@ts-ignore
key={[field.key, "quantity"]}
noStyle
>
<Input placeholder="Quantity" />
</Form.Item>
),
}];
const GroupForm = () => {
const onFinish = (values) => {
console.log(values);
};
//Create form fields based off how many items are in the order
const itemInputs = order.items.map((item) => {
return {
item_code: item.item_code,
quantity: item.quantity,
special_requirements: item.special_requirements,
};
});
return (
<div>
<Form onFinish={onFinish}>
<b>{"Order " + order.po_number}</b>
<Form.Item name="carrier" label="carrier" initialValue={order.carrier}>
<Input style={{ width: "500px" }} />
</Form.Item>
<Form.Item
name="po_number"
label="PO number"
initialValue={order.po_number}
hidden
>
<Input />
</Form.Item>
<b>Order Items</b>
<Form.List name="items" initialValue={itemInputs}>
{(fields, { add, remove }, { errors }) => (
<>
{/* This is where to put the table. As a data source i used the fields */}
<Table dataSource={fields} columns={columns} pagination={false} />
<Form.Item>
<Button
type="dashed"
onClick={() => add()}
style={{ width: "60%" }}
icon={<PlusOutlined />}
>
Add field
</Button>
<Form.ErrorList errors={errors} />
</Form.Item>
</>
)}
</Form.List>
<Form.Item>
<Button type="primary" htmlType="submit">
{" "}
Change Details{" "}
</Button>
</Form.Item>
</Form>
</div>
);
};https://stackoverflow.com/questions/65930098
复制相似问题