如何使用angular 6根据mat表格中的条件仅显示少数列?
我有一张有几列的垫子桌子。这里有一个条件,我需要隐藏两列。对于普通表,我们将使用简单的ngIf条件。但这张垫子桌子似乎不起作用。我试过这个如何使用angular 6根据mat表格中的条件仅显示少数列?,angular,typescript,angular6,mat-table,Angular,Typescript,Angular6,Mat Table,我有一张有几列的垫子桌子。这里有一个条件,我需要隐藏两列。对于普通表,我们将使用简单的ngIf条件。但这张垫子桌子似乎不起作用。我试过这个 displayedColumns1: any[] = [ 'Ref No', 'E Type', { def: 'Approve', showMobile: true }, { def: 'Transfer', showMobile: false }, ]; getDisplayedColumns(): string[] {
displayedColumns1: any[] = [
'Ref No', 'E Type',
{ def: 'Approve', showMobile: true },
{ def: 'Transfer', showMobile: false },
];
getDisplayedColumns(): string[] {
const isMobile = this.entityRole === 'Admin';
return this.displayedColumns1
.filter(cd => !isMobile || cd.showMobile)
.map(cd => cd.def);
}
我的HTML:
<table mat-table [dataSource]="dataSource" class="">
<tbody>
<ng-container matColumnDef="Ref No">
<th mat-header-cell *matHeaderCellDef> Ref No <br>
</th>
<td mat-cell *matCellDef="let element"> {{ ref }} </td>
</ng-container>
<ng-container matColumnDef="E Type">
<th mat-header-cell *matHeaderCellDef> E Type <br>
</th>
<td mat-cell *matCellDef="let element"> {{ etype }} </td>
</ng-container>
<ng-container matColumnDef="Approve">
<th mat-header-cell *matHeaderCellDef> Approve <br>
</th>
<td mat-cell *matCellDef="let element"> {{ Approve}} </td>
</ng-container>
<ng-container matColumnDef="Transfer">
<th mat-header-cell *matHeaderCellDef> Transfer <br>
</th>
<td mat-cell *matCellDef="let element"> {{ Transfer }} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="getDisplayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: getDisplayedColumns;">
</tr>
</tbody>
</table>
参考号
{{ref}}
E型
{{etype}}
批准
{{批准}
转移
{{Transfer}}
但它给出了错误类型错误:无法读取未定义错误的属性“diff”。有谁能建议我如何在mat表中执行此操作吗?您的问题是,您的列名数组将自定义对象与字符串混合在一起,而您的筛选器没有考虑到这一点
displayedColumns1: any[] = [
'Ref No',
'E Type',
{ def: 'Approve', showMobile: true },
{ def: 'Transfer', showMobile: false }
];
getDisplayedColumns(): string[] {
const isMobile = this.entityRole === 'Admin';
return this.displayedColumns1
.filter(cd => !isMobile || cd['showMobile'] !== false)
.map(cd => typeof cd === 'string' ? cd : cd.def);
}
我在你的代码中没有看到所谓的
diff
甚至dif
。如果您有关于diff
的错误,您应该包括导致错误的代码。@Igor yes我的代码中也没有任何称为diff的内容。或者,根据某些条件,你能建议我如何显示几个列吗?我不能在mat table@igortanks中使用ngIF作为您的时间和答案,我没有收到任何错误。但还是有两列没有隐藏。。你能帮忙吗?