I have a Angular Material Table. When I surround html for the table with a <div *ngIf="true">
the table renders but the data no longer sorts when clicking on the header column.
Taking the example from: https://material.angular.io/components/table/overview#sorting
And modifying it, just by adding the <div *ngIf="true"> ... </div>
demonstrates this behavior. Example is at: https://stackblitz.com/edit/angular-quzvjv
执行控制台登录ngOnInit
的this.sort
角没赶上MatSort
组件作为初始化阶段NgIf没有处理的模板。
更改ngOnInit
到ngAfterViewInit
和预期它会奏效。
Base on the solution Angular 5 Material Data Table sorting not working
In Angular 8, the @ViewChild
decorator need a second argument {static: true|false}
In order to catch MatSort
component after DOM rendered, set {static:false}
:
export class TableSortingExample{
sort;
@ViewChild(MatSort, {static: false}) set content(content: ElementRef) {
this.sort = content;
if (this.sort){
this.dataSource.sort = this.sort;
}
}
}
When set {static : true}
, Angular Compiler consider this @ViewChild
element is static, so it only obtain the element one time at ngOnInit()
. Even if *ngIf
triggered still can't catch the MatSort
component.
See https://stackoverflow.com/a/56359612/11777581 by @PierreDuc
感谢DANIL,你的建议的工作,但只有当* ngIf评估为真时的观点初始化。
我现在用的解决方案是:
@ViewChild(MatSort) set content(sort: MatSort) {
this.dataSource.sort = sort;
}
这是根据信息来源: 中* ngIf角2 @ViewChild
:在全部有效的解决方案https://stackblitz.com/edit/angular-quzvjv-jzdbb6
在角8,我们可以很容易地实现* ngIf内多个/单一材料表排序和分页。 在下面的例子中,我使用“NG-模板”,以显示/使用* ngIf其他隐藏的表。
我的样本* ngIf格式
<div *ngIf="!isDataLoadedFromApi; else loadTransactions">No data to display</div>
下面的表是相同的形式
表格1
<ng-template #loadCases>
<table mat-table [dataSource]="snowDataSource" #snowSort="matSort" matSort class="mat-elevation-z3 snow-table">
<mat-paginator class="mat-elevation-z3" #snowPaginator [pageSizeOptions]="[3, 10, 20]" showFirstLastButtons>
</mat-paginator></ng-template>
表2
<ng-template #loadTransactions>
<table mat-table [dataSource]="transactionDataSource" #transactionSort="matSort" matSort
class="mat-elevation-z3 transaction-table">
<mat-paginator class="mat-elevation-z3" #transactionPaginator [pageSizeOptions]="[3, 10, 20]"
showFirstLastButtons>
</mat-paginator></ng-template>
检查“#transactionSort =” matSort“和#transactionPaginator在表-2。另外‘#snowSort =’matSort”和#snowPaginator于表1
在TS文件中配置视图的孩子和其他设置,如下面。 而已。
@ViewChild('snowSort', { static: false }) set matSnowSort(snowSort: MatSort) {
this.snowSort = snowSort;
if (this.snowDataSource !== null && this.snowDataSource !== undefined) {
this.snowDataSource.sort = this.snowSort;
}}
@ViewChild('snowPaginator', { static: false }) set matSnowPaginator(snowPaginator: MatPaginator) {
this.snowPaginator = snowPaginator;
if (this.snowDataSource !== null && this.snowDataSource !== undefined) {
this.snowDataSource.paginator = this.snowPaginator;
}}
@ViewChild('transactionSort', { static: false }) set matTransactionSort(transactionSort: MatSort) {
this.transactionSort = transactionSort;
if (this.transactionDataSource !== null && this.transactionDataSource !== undefined) {
this.transactionDataSource.sort = this.transactionSort;
}}
@ViewChild('transactionPaginator', { static: false }) set matTransactionPaginator(transactionPaginator: MatPaginator) {
this.transactionPaginator = transactionPaginator;
if (this.transactionDataSource !== null && this.transactionDataSource !== undefined) {
this.transactionDataSource.paginator = this.transactionPaginator;
}}
private transactionSort: MatSort;
private transactionPaginator: MatPaginator;
private snowSort: MatSort;
private snowPaginator: MatPaginator;
payIdTransactions: TLItem[];
snowCases: Cases[];
transactionDataSource: MatTableDataSource<TLItem>;
snowDataSource: MatTableDataSource<Cases>;
private getTransactions(startDate: string, endDate: string) {
this.payIdTransactions = results.getTransactionsResponse.TLItems.TLItem;
this.transactionDataSource = new MatTableDataSource(this.payIdTransactions);
}
private getSnowCases(startDate: string, endDate: string) {
this.snowCases = results.getCasesResponse.cases;
this.snowDataSource = new MatTableDataSource(this.snowCases);
}
文章来源: Angular Material Table Sorting - Data Doesn't sort when inside a *ngIf condition