I have bind mat-autocomplete control (almost 30k records ) inside mat-table. Here user is allowed to change values in auto complete and save the mat - table.
If user choose any different values in auto complete control in multiple rows of the mat-table and save.
If we re-bind the mat-table all the mat-autocomplete selected items are showing with last value from the mat-autocomplete.
But here the data source object is updated properly.
Update and Save values in mat-autocomplete
After refreshing the Mat-table setting last value. [ Here the data source is fine, json object having correct values ]
Html Code
<div class="ScrollStyle">
<mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- MaterialDescription Column -->
<ng-container matColumnDef="Gedis Class">
<mat-header-cell *matHeaderCellDef>Gedis Class</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element.GedisClassCode}} </mat-cell>
</ng-container>
<!-- ItemClass Column -->
<ng-container matColumnDef="ItemClass">
<mat-header-cell *matHeaderCellDef> Item Class </mat-header-cell>
<mat-cell *matCellDef="let element">
<mat-autocomplete #sfAuto="matAutocomplete" (optionSelected)="element.ItemClassId = $event.option.viewValue" [displayWith]="valueMapper">
<mat-option *ngFor="let sf of filteredlistOfItemClass" [value]="sf.ItemClassId">
{{sf.ItemClassId}}
</mat-option>
</mat-autocomplete>
<mat-form-field floatLabel="never">
<input matInput placeholder="NA000" #sfInput [formControl]="itemClassControl" [matAutocomplete]="sfAuto"
(input)="itemClassOnChange($event.target.value)">
</mat-form-field>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns" [ngClass]="mat-header-cell"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
Typescript Code
---------------
itemClassOnChange(val) {
this.filteredlistOfItemClass = [];
const value = val;
const filterValue = value.toLowerCase();
if (filterValue && !'') {
this.filteredlistOfItemClass = this.listOfItemClass.filter(
x =>
`${x.ItemClassId}`.toLowerCase().startsWith(filterValue)
);
this.sfInputTrigger.openPanel();
}
}
//Used for binding selected Item class to the Itemclass auto suggest control
public valueMapper = (key) => {
let selection = this.filteredlistOfItemClass.find(e => e.ItemClassId === key);
if (selection)
return selection.ItemClassId;
else
return "NA000";
};
}
The mat-table is placed in container and it is placed in tab control, on click of tab page we are loading and binding the mat-table