如何将单个形式的两个部分组合成角度?

问题描述:

我正在使用角度动态形式进行角度应用,我通过JSON加载动态表单的数据..

I am making angular application with angular dynamic form where i am loading data for dynamic form through JSON..

JSON有两个部分,例如第1部分和第2部分

JSON has two parts such as part 1 and part 2,

  jsonDataPart1: any = [
    {
      "elementType": "textbox",
      "class": "col-12 col-md-4 col-sm-12",
      "key": "project_name",
      "label": "Project Name",
      "type": "text",
      "value": "",
      "required": false,
      "minlength": 3,
      "maxlength": 20,
      "order": 1
    },
    {
      "elementType": "textbox",
      "class": "col-12 col-md-4 col-sm-12",
      "key": "project_desc",
      "label": "Project Description",
      "type": "text",
      "value": "",
      "required": true,
      "order": 2
    }
  ]

  jsonDataPart2: any = [
            {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "property_one",
          "label": "Property One",
          "type": "text",
          "value": "",
          "required": true,
          "order": 3
        },
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "property_two",
          "label": "Property Two",
          "type": "text",
          "value": "",
          "required": true,
          "order": 4
        },
        {
          "elementType": "radio",
          "class": "col-12 col-md-3 col-sm-12",
          "key": "property_required",
          "label": "Property Required",
          "options": [
            {
              "key": "required",
              "value": "Required"
            },
            {
              "key": "not_required",
              "value": "Not Required"
            }
          ],
          "order": 5
        },
        {
          "elementType": "checkbox",
          "class": "col-12 col-md-3 col-sm-12",
          "key": "property_check",
          "label": "Property Required",
          "order": 6
        }
  ]

我正在单独加载JSON部分喜欢,

And i am loading the JSON as separate part like,

  ngOnInit() {
    //Building form first part
    this.questions = this.service.getQuestions(this.jsonDataPart1)
    this.form = this.qcs.toFormGroup(this.questions);

        //Building form second part
    this.questionsPartTwo = this.service.getQuestions(this.jsonDataPart2)
    this.formPartTwo = this.qcs.toFormGroupPartTwo(this.questionsPartTwo);
  }

HTML看起来像,

<div>
    <form (ngSubmit)="onSubmit()" [formGroup]="form">

 <h4> <b> Form Part One begins from here </b> </h4>
        <div *ngFor="let question of questions" class="form-row">
            <ng-container>
                <app-question [question]="question" [form]="form"></app-question>
            </ng-container>
        </div>

 <h4> <b> Form Part Two begins from here </b> </h4>

    <div *ngFor="let partTwo of questionsPartTwo">
                <ng-container>
                <app-question [question]="partTwo" [form]="formPartTwo"></app-question>

            </ng-container>
        </div>

        <div class="form-row">
            <button type="submit" [disabled]="!form.valid">Save</button>
    </div>
  </form> <br>

  <pre>
{{form?.value|json}}
</pre>
</div>

我需要将这两者结合起来,并且需要获得整个单一表单的单个输出..

I need to combine these two and need to get single output for the whole single form..

在我的实际应用程序中,我有两个json数据,并分别加载和分配表格,所以请不要突破第一部分和第二部分json ..

In my real application i am having two json data and loading each separately and assigning form so please dont break out the part one and part two json..

让我在这里停止代码,因为它很长时间你可能会感到困惑..

Let me stop the code here as its getting long you might get confused..

这是一个有效的stackblitz: https://stackblitz.com/edit/angular-x4a5b6-2rykpo

只需采取解决方法 dynamic-form.component.ts和dynamic-form.component.html 您将获得清楚我做了什么..

Just take a workaround dynamic-form.component.ts and dynamic-form.component.html You will get clear what i have done..

请帮我把分割的JSON加载到 this.service.getQuestions 和获得两个部分并加入最终输出中以提交..

Kindly help me to load the splitted JSON to this.service.getQuestions and get two parts and join both in final output to submit..

如果我在方法上错了,请帮我纠正它,因为我是角度和动态的新手amic form ..它需要是角动态形式, json 只加载它没有变化..

If i am wrong in approach kindly help me to correct it as i am new in angular and dynamic form.. It needs to be in angular dynamic form and json loading only no change in it..

帮助我期待的是在提交表格的同时将第1部分和第2部分结合起来..

Help i am expecting is from combining both parts 1 and 2 while submitting form..

请帮助我,我坚持了很长时间才能实现......

Please help me i am stucking for a long to come out of it..

你可以采取几种方法

1.创建一个formJoin是{form1:form1的问题,form2:form2的问题}

1.-Create a formJoin that was {form1:questions of form1,form2:questions of form2}

在你的ngOnInit

In your ngOnInit

formJoin:FormGroup;
ngOnInit()
{
    this.questions = this.service.getQuestions(this.jsonDataPart1)
    this.form = this.qcs.toFormGroup(this.questions);

    this.questionsPartTwo = this.service.getQuestions(this.jsonDataPart2)
    this.formPartTwo = this.qcs.toFormGroup(this.questionsPartTwo);

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

//In your .html 
<form (ngSubmit)="onSubmit()" [formGroup]="formJoin">

2.-以独特的json加入问题

2.-Join the question in an unique json

this.allquestions=this.service.getQuestions(
        this.jsonDataPart1.concat(this.jsonDataPart2));
this.formJoin2=this.qcs.toFormGroup(this.allquestions);
//get the first question.key in the second form
this.questionBreak=this.jsonDataPart2[0].key;

//In your .html
<form  (ngSubmit)="onSubmit()" [formGroup]="formJoin2">

 <h4> <b> An uniq Form </b> </h4>
    <div *ngFor="let question of allquestions" class="form-row">
       <!--make a break fro the secondForm-->
       <ng-container *ngIf="question.key==questionBreak">
           <h4> <b> Form Part Two begins from here </b> </h4>
       </ng-container>
        <ng-container>
            <app-question [question]="question" [form]="formJoin2"></app-question>
        </ng-container>
    </div>
 </form>

重要说明:您无需在服务toFormGroup和toFormGroupPartTwo中使用两个函数。如果你看到的是同一个函数,你可以用不同的参数提供函数,但函数是相同的。

IMPORTANT NOTE: You needn't have two functions in service toFormGroup and toFormGroupPartTwo. If you see is the same function, you "feed" the function with different arguments, but is the same function.

stackblitz 有两个选项

更新
更新代码以便休息,

Update update code to make a break,