简体   繁体   English

动态添加输入表单字段问题reactjs

[英]Dynamically adding Input form field issue reactjs

I am trying to add one cascaded form(inner) field dynamically on click of a button +Add Content . 我试图在点击按钮+添加内容时动态添加一个级联形式(内部)字段。 The array for the field is getting updated but the view is still same. 该字段的数组正在更新,但视图仍然相同。

However, when I try to add the outer field on click of a button +Add Heading dynamically its getting added with no issues. 但是,当我尝试在点击按钮时添加外部字段+动态添加标题时 ,添加时没有任何问题。 Below is the stackblitz url for reference. 以下是stackblitz网址供参考。 Thanks in advance. 提前致谢。

https://stackblitz.com/edit/react-utjwsu?embed=1&file=index.js https://stackblitz.com/edit/react-utjwsu?embed=1&file=index.js

You're only ever rendering one Content field, and the {this.AddContentInput} isn't valid syntax. 您只渲染一个内容字段,而{this.AddContentInput}是无效的语法。 You can edit the AddContentBox method to render all of the Content fields: 您可以编辑AddContentBox方法以呈现所有Content字段:

Original: 原版的:

...
<FormGroup>
    <Label className="set-label-pos upload-img" for="Heading">Content</Label>
    <input className="form-control" type="text"/>
</FormGroup>
{this.AddContentInput}
...

Replaced with: 替换为:

...
{ this.AddContentFields(element) }
...

and

AddContentFields(element) {
  return element.Content.map(content => {
    return (
      <FormGroup>
        <Label className="set-label-pos upload-img" for="Heading">Content</Label>
          <input className="form-control" type="text"/>
        </FormGroup>
    );
  })
}

Here's an edited version of the app with my changes: https://stackblitz.com/edit/react-lcy2te 以下是我的更改的应用程序的编辑版本: https//stackblitz.com/edit/react-lcy2te

Your code is working fine and the state is updating perfectly. 您的代码工作正常,状态正在完美更新。 The problem is that the Content part is added only once in your code. 问题是Content部分只在代码中添加一次。 I just used a function which you already added in it 我刚刚使用了你已添加的功能

{this.addContentTextBox(element.Content)}

Just replace the AddContentBox function with this: 只需用以下AddContentBox替换AddContentBox函数:

 AddContentBox(){ return this.state.content.map((element,i)=>( <div className="header-content" key={i} > <div className="heading-content-wrapper"> <FormGroup> <Label className="set-label-pos upload-img" for="Heading">Heading</Label> <input className="form-control" type="text"/> </FormGroup> {this.addContentTextBox(element.Content)} {this.AddContentInput} <FormGroup> <Button color="success" onClick={this.AddContentInput.bind(this,i)}>+ Add Content</Button> </FormGroup> </div> </div> )) } 

You have the code in that file, but i think you forgot to add it in the function. 你有该文件中的代码,但我想你忘了在函数中添加它。

Hope it helps :) 希望能帮助到你 :)

You are mixing the content variable. 您正在混合内容变量。 In your function AddContentBox() you are using this.state.content.map(...) 在你的函数AddContentBox()你使用this.state.content.map(...)

this.state.content is an array of object like {Heading: '', Content: [{value: ''}]} this.state.content是一个对象数组,如{Heading: '', Content: [{value: ''}]}

BUT in your function AddContentInput() your are pushing an object inside an object of this array contents[index].Content.push({value:''}) 但是在你的函数AddContentInput()你正在推送一个对象在这个数组contents[index].Content.push({value:''})的对象中contents[index].Content.push({value:''})

Depending on your needs you should either push in the root array contents.push({Heading: '', Content: [{value: ''}]} OR iterate the right array in your function AddContentBox() and use this.state.content[0].Content.map(...) 根据您的需要,您应该推入根数组contents.push({Heading: '', Content: [{value: ''}]} 或者在函数AddContentBox()迭代右数组并使用this.state.content[0].Content.map(...)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM