0

I have a FormGroup which has a FormArray Element:

this.addForm = new FormGroup({
  id: new FormControl(''),
  user_id: new FormControl(3),
  client_id: new FormControl('', Validators.required),
  type: new FormControl('', Validators.required),
  description: new FormControl(''),
  payment_ref: new FormControl(''),
  registration_date: new FormControl(''),
  status: new FormControl('', Validators.required),
  sale_date: new FormControl(''),
  payment_date: new FormControl(''),
  total_price: new FormControl(''),
  services: new FormArray([
    new FormGroup({
      service_id: new FormControl('', Validators.required),
      description: new FormControl('', Validators.required),
      unit: new FormControl('', Validators.required),
      price: new FormControl(''),
      quantity: new FormControl(''),
      total_price: new FormControl(''),
    }),
  ])
});

In the GUI i can add a new FormArray by clicking a button. The GUI looks like this: enter image description here

When I select a Servie in the Choose Service part the Description and the price should be filled with data automatically. The code for this part looks like this:

getSelectedService(event,index){
console.log(event.value);
 this.http.get("http://localhost/finance/server/public/api/v1/service" + '/' + event.value)
 .toPromise().then(data => 
  {
    //console.log(data);
    this.getSelectedServiceData = data;
    // console.log(this.getSelectedServiceData.service);
    //console.log(this.addForm.get('services'));
    this.addForm.get('services').patchValue([this.getSelectedServiceData.service]);
    console.log(this.addForm.get('services').value);

  });

}

The problem is that when I add another row and I try to choose from the select box the Service, the description and the price changes but of the first row not of the specified row. Any help?

1 Answers1

1

if you want to access data inside the formarray, you need to use below statement:

(this.addForm.services as FormArray).controls[0].value

Now you need to to patch this value to newly added formarray

Jasdeep Singh
  • 7,901
  • 1
  • 11
  • 28