加载Mat-table时添加微调器?

New*_*iie 15 datasource spinner typescript angular

我在我的材质表中加载数据:

ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}
Run Code Online (Sandbox Code Playgroud)

我想在加载时显示微调器: <mat-spinner ></mat-spinner>

我试试:showSpinner:boolean = true;

ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }  
Run Code Online (Sandbox Code Playgroud)

但我有这个错误:

src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
Run Code Online (Sandbox Code Playgroud)

Tom*_*ula 28

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>

<mat-card *ngIf="isLoading" 
   style="display: flex; justify-content: center; align-items: center">
  <mat-progress-spinner 
    color="primary" 
    mode="indeterminate">
  </mat-progress-spinner>
</mat-card>
Run Code Online (Sandbox Code Playgroud)

table.component.ts

isLoading = true;
dataSource = null;

ngOnInit() {
    this.annuairesService.getMedecins()
       subscribe(
        data => {
          this.isLoading = false;
          this.dataSource = data
        }, 
        error => this.isLoading = false
    );
}
Run Code Online (Sandbox Code Playgroud)

现场演示

  • 它对于分页表来说效果不佳 --&gt; 微调器应该覆盖任何表行(如果有)。 (3认同)

bug*_*ugs 5

showSpinner当您开始请求数据时设置为 true,并在您收到它时将其设置为 false(也就是在subscribe您的服务方法中)

ngOnInit() {
  this.showSpinner = true;
  this.annuairesService.getMedecins()
    .subscribe(res => {
      this.showSpinner = false;
      this.dataSource.data = res;
    });
}
Run Code Online (Sandbox Code Playgroud)