简体   繁体   English

以角度形式将输入创建为数组

[英]Create inputs as array in angular form

Tried my level best please help me out.. 尝试过最好的水平,请帮助我。

I am making an angular dynamic form with a form splitting into total of three parts in which the two parts were made already. 我正在制作一个角度动态表格,其中的表格共分为三个部分,其中两个部分已经制作完毕。 Now I am making part three which will be generated by selecting an option from dropdown... 现在,我将制作第三部分,这是通过从下拉列表中选择一个选项来生成的。

Even those part also is done... 即使那部分也完成了...

But I am unable to make a form array in it... As I am new in angular please help me. 但是我无法在其中创建一个表单数组。由于我是新用户,请帮助我。

HTML: HTML:

Form part 3 will be here which will be array 表格第3部分将在此处为数组

 <select multiple (change)="changeEvent($event)">
  <option *ngFor="let opt of persons" [value]="opt.key">{{opt.value}}</option>
</select>

<div *ngFor="let item of array">
        {{item.value}} is the parent
  <div *ngFor="let child of item.templateChild">
    {{child.property_name}}
        <div *ngFor="let partThree of questionsParthree">
            <ng-container>
            <app-question [question]="partThree" [form]="formPartThree"></app-question>
        </ng-container>
    </div>
    </div>
  </div>

Select Box change event : 选择Box更改事件

  changeEvent(e) {
    if (e.target.value == 1) {
      this.array = [];
      this.array.push(
        {
          key: 1, value: "Template one",
          templateChild: [
            { property_name: "Property one" },
            { property_name: "Property two" }
          ]
        }
      );
      let propertiesArray = [];
      this.array.forEach(element => {
        element.templateChild.forEach(data => {
          propertiesArray.push(
            {
              key: data.property_name,
              label: data.property_name,
              "elementType": "textbox",
              "type": "text"
            }
          )
        });
      });
      this.questionsPartThree = this.service.getQuestions(propertiesArray);
      this.formPartThree = this.qcs.toFormGroup(this.questionsPartThree);
          this.formJoin = new FormGroup({ form1: this.form, form2: this.formPartTwo, form3: this.formPartThree });
    }
   }

Continuation like part 1 and 2.. 像第1部分和第2部分一样继续。

I have posted the code related to creating the form array alone.. 我已经发布了与单独创建表单数组有关的代码。

Updated Stackblitz https://stackblitz.com/edit/angular-x4a5b6-mnyifs 更新了Stackblitz https://stackblitz.com/edit/angular-x4a5b6-mnyifs

Here if we select any option then you will get the input boxes with values.. 在这里,如果我们选择任何选项,那么您将获得带有值的输入框。

I would like to create array with it like, 我想用它来创建数组,

If we select the first option Template One , Output expected is exactly as like 如果我们选择第一个选项Template One ,则预期输出与

"templateChild" : [
{"property_one": "", "property_two":""}
]

So the final output of whole form going to be if i select Template One and also Template Two from select box (as select box is multi select), 因此,如果我从选择框中选择“ Template One和“ Template Two ,则整个表单的最终输出将是(因为选择框是多选),

{
  "form1": {
    "project_name": "",
    "project_desc": ""
  },
  "form2": {
    "property_one": "",
    "property_two": ""
  },
    "template_details" : [
    { "template_name": "Template One",
      "templateChild" : [{"property_one": "", "property_two":""}]
    },
    { "template_name": "Template Two",
      "templateChild" : [{"property_three": "", "property_four":"", 
                            "property_five":""}]
    }
    ]
}

Have a work around in the demo i have provided and give me a better solution.. 我提供的演示中有一些工作,可以为我提供更好的解决方案。

Kindly please help me to create a form as like the above when we select an option from dropdown.. If i am wrong in my approach also please correct me.. 请从下拉菜单中选择一个选项,如上所示,请帮助我创建一个表格。.如果我的方法不对,也请纠正我。

If i finish this third part then everything will get alright any angular technical expert please help me.. 如果我完成了这第三部分,那么任何有角度的技术专家都可以解决,请帮助我。

Taking too long please help me out.. 花费太长时间请帮助我。

You can dynamically change an AbstractController inside a FormGroup using the setControl() method. 您可以使用setControl()方法在setControl()动态更改AbstractController。

Add an empty form3 part for the moment 现在添加一个空的form3零件

this.form3 = new FormGroup({});

this.formJoin = new FormGroup({ form1: this.form, form2: this.formPartTwo, form3: this.form3 })

When selecting an item, generate a new FormGroup according the form you create. 选择一个项目时,根据您创建的表单生成一个新的FormGroup。

if (e.target.value == 1) {
  this.array = [];
  this.form3 = new FormGroup({'Property one': new FormControl('insert whatever you want')});
  this.formJoin.setControl('form3', this.form3);

You should be able to do something what that start! 您应该能够开始做些什么!

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

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