1

我希望我的表单以数组格式提交价格数据,目前,我的表单有一张地图,看起来像

{data.Type && 
    <div>
      {data.Type.map((datamapped)=> 

        <div key={datamapped._id}>
        <p>{datamapped.TypeName}</p>

           <Form.Item>

          {getFieldDecorator(`price.${datamapped._id}.basePrice`)(
            <Input placeholder="Base Price"/>,
          )}

          {getFieldDecorator(`price.${datamapped._id}.higherPrice`)(
            <Input placeholder="Higher Price"/>,
          )}

        </div>
      )}
    </div>
  }

在这里,我在这里映射我的类型并包括 basePrice 和 highPrice 字段

结果是:

price: 
        { 
           '5dc2913cf9e2372b11db4252': { basePrice: '0', higherPrice: '0' },
           '5dc2a109f9e2372b11db4253': { basePrice: '0', higherPrice: '0' } 
        },

我想把上面的结果做成数组格式怎么办呢?

4

4 回答 4

2

尝试更改datamapped._id[index]

{data.Type && 
    <div>
      {data.Type.map((datamapped, index)=> 

        <div key={datamapped._id}>
        <p>{datamapped.TypeName}</p>

           <Form.Item>

          {getFieldDecorator(`price[${index}].basePrice`)(
            <Input placeholder="Base Price"/>,
          )}

          {getFieldDecorator(`price[${index}].higherPrice`)(
            <Input placeholder="Higher Price"/>,
          )}

        </div>
      )}
    </div>
  }
于 2019-11-08T07:34:35.500 回答
0

你可以试试这个:

var json = {
  price: {
    "5dc2913cf9e2372b11db4252": { basePrice: "0", higherPrice: "0" },
    "5dc2a109f9e2372b11db4253": { basePrice: "0", higherPrice: "0" }
  }
};

json.price = Object.entries(json.price);
console.log(json);
于 2019-11-08T07:30:16.667 回答
0

我的动态表格的笔记

antd@3.x

const MyForm = ({ form = {} }) => {
  const { getFieldValue, getFieldDecorator } = form;
  const [defaultData, setDefaultData] = useState({});
  const add = ()=>{
    // 
    defaultData.price.push({basePrice: 'zz',
      higherPrice: 'hzz',key: new Date().getTime()})
  }
  return (
    <Form>
      <Form.Item>
        {Array.isArray(defaultData.price) && // 
          defaultData.price.map((datamapped, index) => {
            return (
              <div key={datamapped.id||datamapped.key}>
                {getFieldDecorator(`price[${index}].basePrice`, { // 
                  initialValue: datamapped.basePrice,
                })(<Input placeholder="Base Price" />)}

                {getFieldDecorator(`price[${index}].higherPrice`, {
                  initialValue: datamapped.higherPrice,
                })(<Input placeholder="Higher Price" />)}
              </div>
            );
          })}
      </Form.Item>
      <Button onClick={add}> + </Button>
    </Form>
  );
};

export default Form.create()(MyForm);

数据结构

|-- price array[object]
  |-- basePrice string
  |-- higherPrice string

必须使用 useState 辅助 setFieldsValue

const [defaultData, setDefaultData] = useState({})
setDefaultData({
  price: [
    {
      basePrice: 'xx',
      higherPrice: 'hxx',
    },
    {
      basePrice: 'yy',
      higherPrice: 'hyy',
    },
  ],
});

⚠️ setFieldsValue 仅在第一次调用时有效

于 2021-06-09T10:55:19.477 回答
0

将值作为数组获取的另一种方法是提供一个数组作为 FormItem 名称属性,如下所示:

<FormItem name={['wrapperName', index, 'propertyName']}>
    <Input/>,
</FormItem>
于 2021-07-09T13:43:06.660 回答