Angular 8, заполнить вложенный FormArray - PullRequest
0 голосов
/ 27 мая 2020

Я получаю данные с сервера в следующем формате:

{
  "name": "Company",
  "Division": [
    {
      "name": "ABC",
      "position": "XYZ"
    }
  ],
  "employees": [
    {
      "name": "emp_1",
      "FullName": [
        {
          "firstName": "John",
          "lastName": "smith"
        }
      ]
    }
  ]
}

Я хочу заполнить данные в форме. Таким образом, пользователь может редактировать форму. Я могу заполнить массив подразделений и сотрудников. Я не могу заполнить массив FullName, который находится внутри массива сотрудников.

.ts код -

editForm = this.fb.group({
name: [],

Division: this.fb.array([
  this.newDivision()
]),


employees: this.fb.array([
  this.newEmployees()
])

 });

  Division(): FormArray {
     return this.editForm.get("Division") as FormArray
  }

 newDivision(): FormGroup {
   return this.fb.group({
        name: [],
        position: []
   })
 }

 addDivision() {
   this.Division().push(this.newDivision());
 }

 removeDivision(i: number) {
   this.Division().removeAt(i);
 }

 employees(): FormArray {
  return this.editForm.get("employees") as FormArray
 }


 newEmployees(): FormGroup {
   return this.fb.group({
     name: [],
     FullName: this.fb.array([
        this.newFullName()
     ])
   })
 } 

addEmployees() {
   this.attributes().push(this.newEmployees());
}

removeEmployees(index: number) {
  this.Employees().removeAt(index);
}

FullName(index: number): FormArray {
   return this.employees().at(index).get("FullName") as FormArray
}

 newFullName(): FormGroup {
   return this.fb.group({
     language: [],
      caption: [],
   })
}

addFullName(index: number) {
   this.FullName(index).push(this.newFullName());
}

removeFullName(index: number, nameIndex: number) {
  this.FullName(index).removeAt(nameIndex);
}

ngOnInit(){
    this.editForm.patchValue({
      name: this.entity.name,
    });

    this.editForm.setControl('Division', this.setDiv(this.div));

    this.editForm.setControl('employees', 
    this.setEmp(this.entity.employees));

    setEmp(getEmp: any): FormArray {
      const formArray = new FormArray([]);
       getEmp.forEach(s => {
         formArray.push(this.fb.group({
         name: s.name
         }));
       });
      return formArray;
     }

    setDiv(getDiv: any): FormArray {
       const formArray = new FormArray([]);
       getDiv.forEach(s => {
         formArray.push(this.fb.group({
           name: s.name,
           position: s.position,
         }));
       });
       return formArray;
    }
 }

Как мне использовать setControl для массива FullName?

1 Ответ

0 голосов
/ 28 мая 2020

Так я решил проблему.

setEmp(getEmp: any): FormArray {
const formArray = new FormArray([]);
getAEmp.forEach(s => {
  formArray.push(this.fb.group({
    name: s.name,
    FullName:this.fb.array([
    this.fb.group({
      firstName:s.FullName[0].firstName,
      lastName:s.FullName[0].lastName
    })
    ]),
  }));
});
return formArray;

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...