I am loading data into the mat-table data source from an API. There is an addnewrow() method which would add a new row at the top. I am actually trying to make the first column of the table editable. (i.e) user can give input only if they create a new row. The rest of the data in the table should not be editable. So for that I use a local flag variable false by default. Based on the flag, I am creating ng-template
with ng-container.
. If it's a new row, the first column is the only editable column, otherwise it should not be editable. I do not know why this is not working.
expected:
- Only first column of newly added row should be editable
- save in local storage.
Please check minimal demo - stackblitz, as I don't have rights share actual snippets, my apologies.
Kindly share any working examples and best approach(es) to achieve the goal.
snippet
<table mat-table #methedofaccept [dataSource]="dataSource" class="mat-elevation-z8" id= "tbl">
<ng-container *ngIf= "isColumnEditable == true; then showInputField else normalColumn ">
<th mat-header-cell *matHeaderCellDef> List of Values </th>
</ng-container>
<ng-template #showInputField>
<td mat-cell matColumnDef="title" *matCellDef="let element" >
<mat-form-field >
<input matInput [value]="element.title" [(ngModel)]="element.title">
</mat-form-field>
</td>
</ng-template>
<ng-template #normalColumn>
<div matColumnDef="title">
<td mat-cell *matCellDef="let element"> {{element.title}} </td>
</div>
</ng-template>
<ng-container matColumnDef="action">
<th mat-header-cell *matHeaderCellDef> Action </th>
<td mat-cell *matCellDef="let element">
<i *ngIf="element.endDate == null " class="material-icons clickable" (click)="deleteRow(element)">delete</i>
<i *ngIf="element.endDate != null" class="material-icons clickable ct-blue undo-icon" (click)="undoRow(element)">undo</i>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;" [ngClass]="{'row-deleted': row['isDeleted']" >
</tr>
</table>
ERROR TypeError: Cannot read property 'template' of undefined
Thank you
What i got from your question:
In your stackblitz, make the table-filtering-example.html to be:
in your stackblitz, make the table-filtering-example.ts to be: