在加载 Mat-table 时添加微调器?
Add a spinner when Mat-table is loading?
我像这样在 material table 中加载数据:
ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}
我想在加载时显示微调器:<mat-spinner ></mat-spinner>
我尝试:
showSpinner: boolean = true;
ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }
但是我有这个错误:
src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
在开始请求数据时将 showSpinner
设置为 true,在收到数据时将其设置为 false(也就是在服务方法的 subscribe
中)
ngOnInit() {
this.showSpinner = true;
this.annuairesService.getMedecins()
.subscribe(res => {
this.showSpinner = false;
this.dataSource.data = res;
});
}
table.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- table here ...-->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<div *ngIf="isLoading"
style="display: flex; justify-content: center; align-items: center; background: white;">
<mat-progress-spinner
color="primary"
mode="indeterminate">
</mat-progress-spinner>
</div>
table.component.ts
isLoading = true;
dataSource = null;
ngOnInit() {
this.annuairesService.getMedecins()
subscribe(
data => {
this.isLoading = false;
this.dataSource = data
},
error => this.isLoading = false
);
}
如果您希望微调器覆盖行,例如在分页 table 上,您可以将微调器叠加在 table 和 position: absolute
之前。只需确保在父容器上设置 position: relative
,这样叠加层就不会扩展它的父容器大小。
<div class="mat-elevation-z8 mt-3" style="position: relative;">
<div style="display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.6);
position: absolute;
width: 100%;
height: 100%;
z-index: 1;" *ngIf="isLoading" >
<mat-progress-spinner color="accent" mode="indeterminate" diameter="50">
</mat-progress-spinner>
</div>
<table mat-table [dataSource]="data" matSort matSortActive="createDate"
matSortDisableClear matSortDirection="desc">
....
</table>
<mat-paginator [length]="data_count" [pageSize]="10"></mat-paginator>
</div>
我像这样在 material table 中加载数据:
ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}
我想在加载时显示微调器:<mat-spinner ></mat-spinner>
我尝试: showSpinner: boolean = true;
ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }
但是我有这个错误:
src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
在开始请求数据时将 showSpinner
设置为 true,在收到数据时将其设置为 false(也就是在服务方法的 subscribe
中)
ngOnInit() {
this.showSpinner = true;
this.annuairesService.getMedecins()
.subscribe(res => {
this.showSpinner = false;
this.dataSource.data = res;
});
}
table.component.html
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- table here ...-->
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<div *ngIf="isLoading"
style="display: flex; justify-content: center; align-items: center; background: white;">
<mat-progress-spinner
color="primary"
mode="indeterminate">
</mat-progress-spinner>
</div>
table.component.ts
isLoading = true;
dataSource = null;
ngOnInit() {
this.annuairesService.getMedecins()
subscribe(
data => {
this.isLoading = false;
this.dataSource = data
},
error => this.isLoading = false
);
}
如果您希望微调器覆盖行,例如在分页 table 上,您可以将微调器叠加在 table 和 position: absolute
之前。只需确保在父容器上设置 position: relative
,这样叠加层就不会扩展它的父容器大小。
<div class="mat-elevation-z8 mt-3" style="position: relative;">
<div style="display: flex;
justify-content: center;
align-items: center;
background: rgba(255, 255, 255, 0.6);
position: absolute;
width: 100%;
height: 100%;
z-index: 1;" *ngIf="isLoading" >
<mat-progress-spinner color="accent" mode="indeterminate" diameter="50">
</mat-progress-spinner>
</div>
<table mat-table [dataSource]="data" matSort matSortActive="createDate"
matSortDisableClear matSortDirection="desc">
....
</table>
<mat-paginator [length]="data_count" [pageSize]="10"></mat-paginator>
</div>