简体   繁体   English

角度动态形式,具有从JSON获取的元素组

[英]Angular dynamic form with groups of elements getting from JSON

I am making angular application and building of angular dynamic form. 我正在进行角度应用并构建角度动态形式。

Here i am trying to split of form in two parts such as Person Name and Personal details.. 在这里,我试图将形式分为两部分,例如“人名”和“个人详细信息”。

For Person Name its working for grouping but for Personal details its not working. 对于“个人”,其名称适用于分组,但对于“个人”详细信息,其无效。

The Html : HTML

<div *ngIf="form">
  <div *ngFor="let question of questions" class="form-row" [formGroup]="form">
      <ng-container *ngIf="!question.children">
        <app-question [question]="question" [form]="form"></app-question>
      </ng-container>
      <ng-container *ngIf="question.controlType === 'group' && question.children && question.children.length > 0">
        <app-dynamic-group [questions]="question.children" [form]="form.controls[question.key]" [key]="question.key" [formControlName]="question.key"></app-dynamic-group>
      </ng-container>
  </div>
</div>

JSON : JSON

  jsonData: any = [
    {
      "elementType": "group",
      "key": "person_name",
      "children": [
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "first_name",
          "label": "First Name",
          "type": "text",
          "value": "",
          "required": true,
          "minlength": 3,
          "maxlength": 20,
          "order": 1
        },
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "last_name",
          "label": "Last Name",
          "type": "text",
          "value": "",
          "required": true,
          "order": 2
        }
     ],
    },
        {
      "elementType": "group",
      "key": "personal_details",
      "children": [
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "email",
          "label": "Email",
          "type": "text",
          "value": "",
          "required": true,
          "minlength": 3,
          "maxlength": 20,
          "order": 1
        },
        {
          "elementType": "textbox",
          "class": "col-12 col-md-4 col-sm-12",
          "key": "mobile",
          "label": "Mobile",
          "type": "text",
          "value": "",
          "required": true,
          "order": 2
        }
     ],
    },
  ];

The working Stckblitz : https://stackblitz.com/edit/angular-x4a5b6-5uj52y 工作中的Stckblitzhttps : //stackblitz.com/edit/angular-x4a5b6-5uj52y

As of working everything works fine.. Already a group was made for Person name and its working fine but for Personal details i am unable to find the input boxes.. 一切正常,工作正常。.已经为“个人”名称及其工作建立了一个小组,但是对于“个人详细信息”,我无法找到输入框。

A single form needs to get split up with titles above each part thats the requirement of this form. 单个表格需要拆分,每个部分上方都有标题,这就是该表格的要求。

Here the {{question.key}} displays the name on each input boxes but i need to display only Person Name at top.. Because it is the parent title and the remaining such as First Name , Last Name are input box labels.. How to show the parent title alone in before of each part ( Person Name (Has First and Last Name) , Personal Details (Has Email and Mobile) )... 在这里{{question.key}}在每个输入框上显示名称,但我只需要在顶部显示“ Person Name 。因为它是父标题,其余的如First NameLast Name是输入框标签。如何在每个部分的前面单独显示父标题(“ 人名”(具有名字和姓氏),“个人详细信息”(具有电子邮件和移动电话) )...

I would like to have order split up exactly like the below with title for each respectively. 我想分别按以下顺序分别分配标题。

Person Name 人名

 -> First Name
 -> Last Name

Personal Details 个人资料

 -> Email
 -> Mobile Number

If i am wrong with the above approach then kindly help me to split this https://stackblitz.com/edit/angular-x4a5b6-geesde dynamic form like the below given approach.. 如果我对上述方法有误,请帮助我拆分此https://stackblitz.com/edit/angular-x4a5b6-geesde动态形式,如以下给定的方法。

My form needs to look like this https://stackblitz.com/edit/angular-zc34qr but it needs to be in pure angular dynamic form and JSON loading.. 我的表单需要看起来像这样https://stackblitz.com/edit/angular-zc34qr,但是它必须是纯角度动态表单和JSON加载。

Kindly help me to create a group Personal Details like the Person Name which was already created and working.. 请帮助我创建一个小组Personal Details例如已经创建并正在工作的“ Person Name

Stuck for a long duration in this kindly help me please... 卡住了很长时间,请帮我...

I don't understand why you creates additional formGroup here: 我不明白您为什么在这里创建其他formGroup:

this.form = new FormGroup({main: innerForm});

Just use formGroup you're getting from your service: 只需使用从服务中获取的formGroup:

dynamic-form.component.ts 动态表格。组件

this.form = this.qcs.toFormGroup(this.questions);

dynamic-form.component.html dynamic-form.component.html

<app-dynamic-group [questions]="questions" [form]="form"></app-dynamic-group>

Now, you do not need to implement ControlValueAccessor on your DynamicGroupComponent. 现在,您无需在DynamicGroupComponent上实现ControlValueAccessor。 You're passing FormGroup to it and it should be enough to generate form dynamically. 您将FormGroup传递给它,它应该足以动态生成表单。

dynamic-group.component.ts dynamic-group.component.ts

@Component({
  selector: 'app-dynamic-group',
  templateUrl: './dynamic-group.component.html'
})
export class DynamicGroupComponent {
  @Input() form: FormGroup;

  @Input() questions: QuestionBase<any>[] = [];
}

dynamic-group.component.html dynamic-group.component.html

<div *ngFor="let question of questions" class="form-row">
    <app-question *ngIf="!question.children" [question]="question" [form]="form"></app-question>

    <app-dynamic-group 
       *ngIf="question.controlType === 'group' && question.children && question.children.length"
       [form]="form.get(question.key)"
       [questions]="question.children">
    </app-dynamic-group>
</div>

Forked Stackblitz 分叉的Stackblitz

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

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