Search code examples
javascriptangularpathnested-formsformarray

angular 4 nested formArray / formGroup put value back in form from DB query


I have a nested for array. I want to edit the values in my form so I did a DB query to get the values to edit but I can't put them (personRows3 values) in the right input field. personRows3 is a nested form array which has one or several formgroup (personI3, personR3) see seg_inf below.
I am getting an error:
Error: Cannot find control with path: 'segmentRows3 -> 0 '
Here is the .html code

<form *ngIf="row" [formGroup]="myEditSummaryForm" (ngSubmit)="saveASummary(myEditSummaryForm.value)">
<div class="container">
<div formArrayName="segmentRows3">
  <div *ngFor=" let seg of seg_inf; let i= index " > 
    <div  class="form-group" [formGroupName]="i" >    <---error line----
      <label for="segmentId3">Segment ID {{i+1}}  
        <input type="text" formControlName="segmentId3" id="segmentId3" class="form-control" value="{{i+1}}" >          
      </label>   
      <label><span *ngIf="seg_inf.length > 1" (click)="deleteSegment(i)" class="btn btn-danger">Remove segment</span></label>
      <label for="segmentTime3">Segment time : 
        <timepicker formControlName="segmentTime3" (ngModel)="segmentTime3" [showMeridian]="isMeridian" [showSpinners]="showSpinners"> </timepicker> </label> 
      <button type="button" (click)="addPerson(i)" class="btn btn-info">Add a person</button>
       {{seg.personRows3 |json}}<br>
      <div formArrayName="personRows3">
        <div *ngFor=" let per of seg.personRows3; let j=index " > 
          <div class="form-group" [formGroupName]="j">   #{{j+1}}   
            <label for="personR3">Segment type 
              <input formControlName="personR3" [typeahead]="personRole" type="text" id="personR3" class="form-control">
            </label>
            <label for="personI3">Person name   
              <input formControlName="personI3" [typeahead]="states" type="text" id="personI3" class="form-control">
            </label>
            <label><span *ngIf="seg_inf.length > 1" (click)="deletePerson(i,j)" class="btn btn-warning">Remove</span></label>
          </div>
        </div>
      </div>
      <label for="topic">Topic</label>
        <textarea formControlName="topic" id="topic" class="form-control" (ngModelChange)="onChange($event)"></textarea>  
    </div>            
  </div>
</div>
<button type="button" (click)="addSegment(i)" class="btn btn-primary">Add a segment</button>  
</div>
 <div>
  <button class="btn btn-success form-control" 
(click)="saveASummary(myEditSummaryForm.value)"> Save update(s)</button>
 </div>             
</form> 

Here is the .ts code

for (var i = 0, len = this.seg_inf.length; i < len; i++) {
  let segmentRows3 = <FormArray>this.myEditSummaryForm.get('segmentRows3');
  segmentRows3.push(
    this.fb.group({
    segmentTime3: this.seg_inf[i].segmentTime3, 
    segmentId3: i+1, 
    topic: this.seg_inf[i].topic,    
    PersonRows3: this.getPerson(this.seg_inf, i),       //function call
      })
    );
  }    


 getPerson(s_i: any[], index: number) {
let s_i_prl = s_i[index].personRows3.length;
let s_i_pr = s_i[index].personRows3;
// get all people for that segment
for (var j = 0, lenj=s_i_prl; j < lenj; j++) {
  let personRows3 = <FormArray>this.myEditSummaryForm.get(`segmentRows3.${index}.personRows3`)
  this.fb.array([
    this.fb.group({
        personR3: s_i_pr[j].personR3,
        personI3: s_i_pr[j].personI3, 
        })
    ])
  }
}   


this.seg_inf = [{"segmentTime3":"2018-05-23T14:20:00.051Z","segmentId3":"","topic":"topic1",   
"personRows3":[{"personR3":"HS - host","personI3":"California"},{"personR3":"GS - guest","personI3":"Alaska"}]},   
 {"segmentTime3":"2018-05-23T14:30:00.533Z","segmentId3":"","topic":"topic2",   
 "personRows3":[{"personR3":"HS - host","personI3":"Arizona"}]}]   

Everything works until I try to fill up the inputs fields for the personRows3!
You can see : {{seg.personRows3 |json}} after Add a person button

enter image description here

Here is a nonworking code. What am I missing?

After @yurzui answer:
enter image description here


Solution

  • It shouldn't be so complicated as in your getPerson method that actually doesn't return anything.

    You can go with the following code:

    personRows3: this.getPerson(this.seg_inf[i].personRows3);
    ...
    
    getPerson(personRows: any[]) {
      return this.fb.array(
        personRows.map(p => this.fb.group({
          personR3: p.personR3,
          personI3: p.personI3,
        })
      ))
    }
    

    Here I am returning FormArray of FormGroups which are based on your personRows3 array.

    You can also check it in Ng-run Example