So I am trying to add a new line in a table which is populated from an API but it looks that I miss something. Here is my code:
HTML:
<button mat-flat-button color="primary" class="btn-add"
(click)="addRow()"
[disabled]="isLoading">ADD</button>
<table mat-table [dataSource]="dataSource" class="mat-elevation-z2" *ngIf="show">
<ng-container *ngFor="let column of displayedColumns" [matColumnDef]="column">
<th mat-header-cell *matHeaderCellDef>{{column}}</th>
<td mat-cell *matCellDef="let element; index as i;">
<span *ngIf="element.editing; then editBlock else displayBlock"></span>
<ng-template #displayBlock>{{element[column]}}</ng-template>
<ng-template #editBlock>
<mat-form-field appearance="fill">
<input matInput [(ngModel)]="element[column]">
</mat-form-field>
</ng-template>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
TS file:
displayedColumns: string[] = [];
dataSource: any;
addRow() {
console.log("Adding new row");
let newRow = {
editing: true
};
this.displayedColumns.forEach(x => newRow[x] = "");
this.dataSource.push(newRow);
}