Dynamic id in FormArray Angular

Viewed 618

everything good? I'm working with FormArray and I have the following problem, when trying to get the value of the category it always returns the same value because as it's a formArray the id="category" is fixed. I can currently leave the id dynamic like this: id:"category"+i. But I am not therefore doing a get on this id:"category"+i value. dynamically. The problem is how to get this value dynamically. grateful in advance

Component 

  getCategory() {
    this.nameList = []
    const value = (<HTMLInputElement>document.getElementById("category")).value 
    console.log(this.nameList)
    this.initcategory(value)
  }
  initcategory(value) {
    console.log(value)
    const totalResults = 0;
    const searchModel = {
      text: '',
      status: '',
    };
    this.feedstockService.getFeedstock(totalResults, searchModel)
    .subscribe(response => {
      const feedstocks = response.results.filter(x => x.category === value)
      feedstocks.forEach(feedstock => this.nameList.push(feedstock.name))
     })
  }
HTML


<ng-container  [formGroup]="feedstockForm">
          <ng-container formArrayName="feedstock">
            <accordion [closeOthers]="oneAtATime" *ngFor="let fcFeedStock of feedstockForm.get('feedstock')?.controls; let i = index">
              <accordion-group  heading="{{ fcFeedStock.get('position').value }} / {{ fcFeedStock.get('category').value }}" [formGroupName]="i">
                <div>
                  <i class="material-icons close-category" (click)="removeFeedstockCategory(i)">
                    close
                  </i>
                </div>
                <div class="divide-section">
                  <div class="first-column">
                    <div class="header-section options">
                      <span>ADICIONAR COMPONENTE</span>
                      <i class="material-icons" (click)="addFeedstockComponent(i)">
                        add
                      </i>
                    </div>
                      <div class="form-group">
                        <label for="category">CATEGORIA</label>
                        <select class="form-control" formControlName="category" id="category" (onChange)="getCategory()" (blur)="onCountryChange($event.target.value)">
                          <option value="" selected ></option>
                          <option *ngFor="let cat of categories" [value]="cat.value">
                            {{cat.viewValue}}
                          </option>
                        </select>
                      </div>

1 Answers

Rod, when we manage a FormArray of FormGruoup ( I Imagine you want mananage an array in the way

data=[{position:..category},{position:..,category:...}]

First we make a getter with the array

get categoryArray()
{
   return this.feedstockForm.get('feedstock') as FormArray
}

And make a function that return a FormGroup with the values of the formArray

getCategoryGroup(data:any=null)
{
      data=data ||{position:0,category:''}
      return new FormGroup({
          position:new FormGroup(data.position),
          category:new FormGroup(data.category)
      })
}

See that this function return a FormGroup of FormControls that has the values of the object "data" we pass or an empty FormGroup if we don't pass argument

So, we can create our formGroup like

this.feedstockForm=new FormGroup(
{
      new FormArray(data.map(x=>this.getCategoryGroup(x)
})

Futhermore, to add a new element to the FormArray you can simply (see how we are using the two functions before defined

addElement(){
   this.categoryArray.push(this.getCategoryGroup()))
}
Related