代码之家  ›  专栏  ›  技术社区  ›  David Jeyathilak

从JSON数据的嵌套表单数组填充表单控件中的值

  •  1
  • David Jeyathilak  · 技术社区  · 7 年前

    FormArray formObj有一个componentDetails对象数组,该对象又有一个嵌套的component数组。

       export class FormViewComponent implements OnInit {
    
        public callbackForm: FormGroup;
    
        formObj = {
            "componentDetails": [{
                "component": [{
                    "value": "Choice 1"
                }, {
                    "value": "Choice 2"
                }],
                "cpv": "",
                "label": "Description of Problem",
                "type": "radio",
                "mandatory": true
            }]
        };
    
        loadObservableForm() {
            this.formService.getData(this.id)
                .subscribe(
                    (formObj) => {
                        this.formObj = formObj;
                        this.callbackForm = this._formBuild.group({
                            componentDetails: this._formBuild.array([])
                        });
                        this.addComponentDetails();
                    },
                    (err) => console.error(err),
                    () => console.log('observable complete')
                );
    
        }
    
        addComponentDetails() {
            const control = <FormArray> this.callbackForm.controls.componentDetails;
            this.formObj.componentDetails.forEach(x => {
                control.push(this.addControl(x));
            });
        }
    
        addControl(x) {
            const group = this._formBuild.group({
                label: x.label,
                cpv: x.cpv,
                type: x.type,
                mandatory: x.mandatory,
                component: this._formBuild.array([this.addOptions(x)])
            });
            return group;
        }
    
        addOptions(z) {
            const control = < FormArray > z.component;
            z.component.forEach(y => {
                control.push(this.addOptionRow(y.value));
            });
        }
    
        addOptionRow(value) {
            return this._formBuild.group({
                value: value
            });
        }
    }
    

    模板HTML:

    <form [formGroup]="callbackForm">
        <div>
            <div formArrayName="componentDetails">
                <div *ngFor="let question of callbackForm.controls.componentDetails.controls; let i = index;" [formGroupName]="i">
                <div class="row">
                    <div class="col-md-12 panel-group panel-group--compressed">
                        <div class="panel panel--default">
                            <fieldset>
                                <div class="row" *ngIf="question.controls.type.value === 'radio'">
                                    <div class="col-md-12">
                                        <p>{{ question.controls.label.value }}</p>
                                        <div formArrayName="component">
                                            <div *ngFor="let answer of question.controls.component.controls; let j = index" [formGroupName]="j">
                                            <label class="radio radio--alt radio--stacked">
                                            <input type="radio" name="radio-stacked">
                                            <span class="radio__input"></span>
                                            <span class="radio__label">{{ answer.value }}</span>
                                            </label>
                                        </div>
                                    </div>
                                </div>
                        </div>
                        </fieldset>
                    </div>
                </div>
            </div>
        </div>
        </div>
        </div>
    </form>
    

    问题:

    1 回复  |  直到 6 年前
        1
  •  3
  •   AT82    6 年前

    你打电话时有问题 addOptions 表单数组内部:

    component: this._formBuild.array([this.addOptions(x)])
    

    添加选项 addOptionRow addControl

    addControl(x) {
      const group = this._formBuild.group({
        label: x.label,
        cpv: x.cpv,
        type: x.type,
        mandatory: x.mandatory,
        component: this._formBuild.array([])
      });
      const ctrl = group.controls.component as FormArray;
      x.component.forEach(y => {
        ctrl.push(this._formBuild.group({
          value: y.value
        }))
      })
      return group;
    }
    

    您的模板在其他方面是正确的,但要显示标签,请执行以下操作:

    {{ answer.value.value }}
    

    而不是

    {{ answer.value }}
    

    StackBlitz