以 Antd 形式获取结果作为数组

Get result as Array in Antd Form

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

{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>
  }

我在这里映射我的 Type 并包括 basePrice 和 higherPrice 字段

结果是:

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

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

你可以试试这个:

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

json.price = Object.entries(json.price);
console.log(json);

尝试将 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>
  }

我的动态表单笔记

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 only works in the first calling

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

<FormItem name={['wrapperName', index, 'propertyName']}>
    <Input/>,
</FormItem>