简体   繁体   English

将formGroup的多个数组的数据加载到表单中? 如何?

[英]Load data of multiple array of a formGroup into the form ? how?

I want to load the data into the form, by clicking the edit button.我想通过单击编辑按钮将数据加载到表单中。 Currently, I am able to load only single array data into the form.目前,我只能将单个数组数据加载到表单中。 but unable to load multiple array data.但无法加载多个数组数据。

Here is: https://stackblitz.com/edit/angular-fzgtqc?Sile=src%2Fapp%2Fapp.component.ts这里是: https://stackblitz.com/edit/angular-fzgtqc?Sile=src%2Fapp%2Fapp.component.ts

I have already patched the data into the form but only of a single array, not multiple array.我已经将数据修补到表单中,但只有一个数组,而不是多个数组。

Here is the sample data:这是示例数据:

 data = {
    "trainer_id": "t001",
    "personal_details": {
        "name": {
            "first_name": "ABC",
            "last_name": "Kumar"
        },
        "dob": "1990-12-20",
        "about_yourself": "i am a corporate trainer from 10 years",
        "languages_known": ["Kannada", "English", "Tamil"],
        "willing_to_travel": "yes"
    },
    "technologies": [{
        "name": "Angular 2",
        "experience": 4,
        "ratings": 7.9,
        "costing": {
            "freshers": 8000,
            "laterals": 12000,
            "project_specific": 15000
        },
        "work_as_consultant": "yes"
    },
  {
        "name": "Java",
        "experience": 5,
        "ratings": 8,
        "costing": {
            "freshers": 8000,
            "laterals": 12000,
            "project_specific": 15000
        },
        "work_as_consultant": "yes"
    }
  ],
    "certifications": [{
        "title": "Sun Certified",
        "Year": 1999
    }],
};

Below function will be executed when an edit button is clicked下面的 function 将在单击编辑按钮时执行

editTrainner() {
    this._trainnerservice.currentTrainner = this.data;
    this.registrationForm.patchValue({
          personal_details: { type: Object,
            name: { type: Object,
                first_name: this._trainnerservice.currentTrainner.personal_details.name.first_name,
                last_name: this._trainnerservice.currentTrainner.personal_details.name.last_name
            },
            dob: this._trainnerservice.currentTrainner.personal_details.dob,
            about_yourself: this._trainnerservice.currentTrainner.personal_details.about_yourself,
            willingly_to_travel: this._trainnerservice.currentTrainner.personal_details.willingly_to_travel
        }
    });
    this.addlanguages_known();
    this.registrationForm.patchValue({
  technologies: this._trainnerservice.currentTrainner.technologies
});
  this.registrationForm.patchValue({
  certifications: this._trainnerservice.currentTrainner.certifications
});
  }
  addlanguages_known(): any {
    const control = this.registrationForm.get('personal_details.languages_known') as FormArray;
    this._trainnerservice.currentTrainner.personal_details.languages_known.forEach(x => {
        control.push(this.fb.control(x));
      });
  }

In the sample data, you will find "technologies" , in that I am to load one array group, not the second group.在示例数据中,您会发现“技术” ,因为我要加载一个数组组,而不是第二组。 You can check the output in the stackblitz link.您可以在 stackblitz 链接中查看 output。

When you are initializing the form you only have 1 FormGroup in technologies FormArray .当您初始化表单时,您在technologies FormArray FormGroup So when you are patching only 1 value gets rendered.因此,当您修补时,只会呈现 1 个值。 You need to add more FormGroup 's to technologies based on server response then patch.您需要根据服务器响应向technologies添加更多FormGroup ,然后进行修补。 Based on your stackblitz add this as first lines of editTrainner function.根据您的stackblitz ,将此添加为editTrainner function 的第一行。

this._trainnerservice.currentTrainner.technologies.forEach(x => {
    this.addTechnologies();
})

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

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