Dynamic mat-select option in form-array

Viewed 4567

I have a dynamic form group.

sample ui image

Both mat-select are dynamic. When I press the add button, it adds another array of the form with same elements and same dynamic fields.

In the first index of the array of the form group, when I change the value of the mat-select in the CATEGORY column, it will populate the JOB column according to values fetch from the database.

My issue is when I add another array of form group, the mat-select are still dynamic but when I change the value of the mat-select in the 2nd row, column CATEGORY, it changes every all the rows in JOBS column base on the fetch data in the first column.

HTML file

<mat-form-field>
  <mat-select formControlName="categories" 
          (selectionChange)="getJobs($event.value)">
              <mat-option *ngFor="let cat of categories" 
                 [value]="cat.jobCatID">
                {{cat.name}}
              </mat-option>
  </mat-select>
</mat-form-field>
<mat-form-field>
   <mat-select formControlName="jobs">
          <mat-option *ngFor="let job of jobs" [value]="job.jobID">
            {{ job.name}}
          </mat-option>
   </mat-select>
 </mat-form-field>

TS file

getJobs(id){
return this.category.getJob(id).subscribe(
  data => {
      if(data.success){
        this.jobs=data.jobsCat.jobs
      }
  }
)}

is their anyway I can make the jobs variable dynamic, in order to have different objects variable with the same values in the ngFor for each JOBS mat-select?

1 Answers

All good now. I just made jobs into array.

jobs:any[]=[];

declared it like this. Then fetch used the index as parameter for a dynamic array.

Related