【发布时间】:2020-06-10 20:40:26
【问题描述】:
我创建了 mat-table 组件,它通过我粘贴的数据生成动态列,但没有操作图标。
<table mat-table [dataSource]="data" class="mat-elevation-z8">
<ng-container [matColumnDef]="item.columnDef" *ngFor="let item of columns; let i = index">
<th mat-header-cell *matHeaderCellDef>{{ item.header }}</th>
<td mat-cell *matCellDef="let element">{{ element[item.columnDef] }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
Input() 从另一个组件粘贴数据。
columns: Column [] = [
{columnDef: 'faculty_id', header: 'ID'},
{columnDef: 'faculty_name', header: 'Faculty'},
{columnDef: 'faculty_description', header: 'Description'},
{columnDef: 'action', header: 'Actions', actions: [
{type: tableActionsType.Edit, icon: 'edit', matTooltip: 'Edit'},
{type: tableActionsType.Delete, icon: 'delete', matTooltip: 'Delete'}
]}
];
我需要什么:
我想用图标生成列Actions,
-
问题:
我需要在 item.columnDef === 'action' 时检查此循环 *ngFor="let item of columns; 并渲染 mat-icon
<ng-container [matColumnDef]="item.columnDef" *ngFor="let item of columns; let i = index">
<th mat-header-cell *matHeaderCellDef>{{ item.header }}</th>
<td mat-cell *matCellDef="let element">
// if item.columnDef == 'action' then mat-icon
// else {{ element[item.columnDef] }}
<mat-icon aria-hidden="false" aria-label="edit" *ngFor="let icon of item.actions" [matTooltip]="icon.matTooltip" (click)="check()">{{icon.icon}}</mat-icon>
</td>
</ng-container>
【问题讨论】:
-
有关于这个问题的更新吗?
-
@Entertain 是的,我设法解决了。请查看答案。