2

I'm using a mat-table inside a popup. Inside the mat-table there will be rows with mat-autocomplete. The thing is I need to add rows dynamically. I'm using a formGroup inside which there is formArray.

this will be the Model of the FormGroup

   {
  formArray:
    [
      {
         name:"someName",
         material:"someMaterial",
         quantity:5
      },
      {
         name:"someName2",
         material:"someMaterial",
         quantity:5
      },    
    ]

}

First the grid will be empty but when i click a button , it inserts a formGroup inside the formArray (which is data source for the grid). but when that happens the UI completely freezes and i cant do anything .

here is the HTML file Code

<form [formGroup]="positionFormGroup">
    <div class="card-body" style="height: 500px; overflow: auto;"  >
        <table class="full-width" style="overflow: scroll;" mat-table [dataSource]="this.positionFormGroup.get('positionInfos').value" formArrayName = "positionInfos">

            <!-- Material Column -->
            <ng-container matColumnDef="Material">
                <th mat-header-cell *matHeaderCellDef class="text-center align-column">
                    <p [innerHTML]=" 'MachineCapacity.Grid_Label_IdMaterial' | translate"></p>
                </th>
                <td mat-cell *matCellDef="let data; let i = index" [formGroupName]="i">
                    {{data.idMaterial}}

                    <input type="text" formControlName="idMaterial" [matAutocomplete]="materialAuto">
                    <mat-autocomplete #materialAuto="matAutocomplete">
                        <mat-option *ngFor="let option of materialFilteredOptions | async" [value]="option.description"
                            class="auto-option">
                            {{option.description}}
                        </mat-option>
                    </mat-autocomplete>

                </td>
            </ng-container>

            <!-- Quantity Column -->
            <ng-container matColumnDef="Quantity">
                <th mat-header-cell *matHeaderCellDef class="text-center align-column">
                    <p [innerHTML]=" 'MachineCapacity.Grid_Label_Quantity' | translate"></p>
                </th>
                <td mat-cell *matCellDef="let data">
                    {{data.quantity}}
                </td>
            </ng-container>

I'm able to track down the problem. it happens because of the reference I'm using for the mat-auto complete. It works either when I remove the matAutoComplete reference from input tag or when i change the formControlName.

here's TS code :

  ngOnInit(): void {

   // this.positionInfos = new FormArray([]);
    this.positionFormGroup = this.formBuilder.group({
      positionInfos: new FormArray([])
    });

    //this.positionFormGroup.

  }

  addMaterial(){
    let data  = {
      rowID :'f',
      position : 'f',
      idMaterial:'f',
      quantity: 'f',
      isDeleted: 'f',
      isActive:true
    }
    

    this.positionFormGroup.get('positionInfos')?.push(this.formBuilder.group(data))
    //console.log( this.positionFormGroup.get('positionInfos').value)
  }

Please Let me know if im missing something , thanks in advance

Stephen26
  • 84
  • 5

1 Answers1

1

Not related to the reference issue and UI is not freezing at all.

It was happening because of change detection, i think so. That's why we got a feel of UI freeze.

Exactly at this point, [dataSource]="this.positionFormGroup.get('positionInfos').value" it triggers the change detection infinite times

I dunno, why it was triggering infinite times.

Already have an answer for nested form groups.

html

<form [formGroup]="positionFormGroup" novalidate>
  <table formArrayName="positionInfos">

    <div *ngFor="let data of control; let i = index">
      <div [formGroupName]="i">

        <input type="text" formControlName="idMaterial" [matAutocomplete]="materialAuto">
        <mat-autocomplete #materialAuto="matAutocomplete">
          <mat-option *ngFor="let option of materialFilteredOptions | async" [value]="option.description">
            {{option.description}}
          </mat-option>
        </mat-autocomplete>
      </div>
    </div>

  </table>
</form>

ts

export class AppComponent {
  positionFormGroup: FormGroup;
  materialControl: FormControl = new FormControl('');
  materialFilteredOptions: Observable<{ description: string; }[]>;
  options = [
    { description: 'item1' },
    { description: 'item2' },
    { description: 'item3' },
    { description: 'item4' }
  ]

  control: any;
  constructor(private formBuilder: FormBuilder) {
  }

  ngOnInit(): void {
    this.materialFilteredOptions = this.materialControl.valueChanges
      .pipe(
        startWith(''),
        map(value => this.options)
      );
    this.positionFormGroup = this.formBuilder.group({
      positionInfos: new FormArray([])
    });

    this.addMaterial();
    this.control = this.getControls().controls;
  }


  addMaterial() {
    (this.positionFormGroup.get('positionInfos') as FormArray).push(this.formBuilder.group({
      rowID: '0',
      position: '0',
      idMaterial: 'item1',
      quantity: 1,
      isDeleted: false,
      isActive: true
    }));
  }

  confirm(){
    console.log( this.positionFormGroup.get('positionInfos').value)
  }

  getControls(){
    console.log( (this.positionFormGroup.get('positionInfos') as FormArray));
    return (this.positionFormGroup.get('positionInfos') as FormArray);
  }
Ram
  • 117
  • 1
  • 1
  • 10