简体   繁体   English

如何跟踪“Ant Design-Dynamic Form Item”并将所有值存储在单个对象状态中?

[英]how can i keep track of "Ant Design- Dynamic Form Item" and store all values in a single object state?

So I'm using Ant Design Dynamic Form Item and you can create as many as input field you want.所以我正在使用 Ant Design 动态表单项,您可以创建任意数量的输入字段。

the question is how can I store all of the values (static field and new fields) in a single array of objects inside my state?问题是如何将所有值(static field and new fields)存储在我的状态内的单个array of objects

Please take a Look at my component请看一下我的组件

if you click add it will create another Autocomplete and a inputfield.如果您单击添加,它将创建另一个自动完成和一个输入字段。 and you enter data然后你输入数据

for example I want an array of object like this例如我想要一个这样的对象数组

[{commisionFor: 'Invoicing' , perctange: 10},{newcondition: 'something' , percentage: 32},{} , {} , {] , and etc ]

I wanna achieve this.我想实现这个。 how can i do this ?我怎样才能做到这一点 ? I will appreciate your help我会感谢你的帮助

PS : I'm not typing my code here because it's to complex and I don't even know what i did. PS :我没有在这里输入我的代码,因为它很复杂,我什至不知道我做了什么。 Take look at this Ant Design Dynamic Form Item sandbox看看这个 Ant Design Dynamic Form Item 沙箱

Ant Dynamic Form Item CodeSandBox Ant 动态表单项 CodeSandBox

you can use onChange event to store all values separately.您可以使用 onChange 事件分别存储所有值。

const [values, setValues] = useState({})

const onChangeHandler = (name, value) =>{
   let data = values;
    data[name] = value;

   setValues(data)
}

and in your multiple input field, create unique name for each input field.并在您的多个输入字段中,为每个输入字段创建唯一名称。

 fields.map((name, index) => {
    <Input placeholder={name} name={name} onChange={(e) => onChangeHandler(name, e.target.value)} />
})

use your own attribute for naming fields or just use the index value.使用您自己的属性来命名字段或仅使用索引值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自定义 Ant 设计的表格? - How can I customised the form of Ant Design? 如何在 Ant 设计表列渲染中获取单个项目的 ID - How can I get single item's id in Ant Design table column rendering 如何在 Ant 设计的 Form.Item 中为 DatePicker 使用 initialValue - How can I use initialValue for DatePicker in Form.Item of Ant Design 我如何从表单提交的多个输入框中获取值并将这些值作为对象存储在React的状态数组中? - How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React? 如何将所有数组值存储到单个 React state 中? - How to store all array values into a single React state? 我可以使用 useRef Hook 还可以用来跟踪以前的状态值吗 - Can I use useRef Hook can also be used to keep track of previous state values React with Ant Design - 如何在多个人之间共享状态<Select>在一个<Form>? - React with Ant Design - How to share state between multiple <Select> on a <Form>? 如何为动态表单字段设置initialValue(蚂蚁设计)? - How to set initialValue for dynamic form field (ant design)? 如何为 react localStorage 中的每个项目存储 state? - How can I store state for every item in react localStorage? 如何添加动态表单元素但保留其值(JS) - How do I add dynamic form elements but keep their values (JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM