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