Add a spinner when Mat-table is loading?
Asked Answered
S

4

43

I load the data in my material table like that :

ngOnInit(){ return this.annuairesService.getMedecins().subscribe(res => this.dataSource.data = res);}

I want show the spinner when is loading : <mat-spinner ></mat-spinner>

I try : showSpinner: boolean = true;

ngOnInit(){ return this.annuairesService.getMedecins()
.subscribe(res => this.dataSource.data = res);
this.dataSource.subscribe(() => this.showSpinner = false }  

but i have this error :

src/app/med-list/med-list.component.ts(54,21): error TS2339: Property 'subscribe' does not exist on type 'MatTableDataSource<{}>'.
Stylographic answered 26/7, 2018 at 8:54 Comment(0)
B
75

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
    );
}

Live demo

Beef answered 26/7, 2018 at 9:4 Comment(6)
Glad I could help :)Beef
It is normal if i can't use my table during 2 secondes after the loading of spinner ?Stylographic
I don't think it is. If you cannot fix it maybe post it as a separate question? Make sure to include stackblitz example.Beef
It is not really working well for a paged table --> the spinner should overlay any table rows, if any.Acetylate
worked for me, i used <mat-progress-bar mode="indeterminate"></mat-progress-bar>Harrietteharrigan
How to implement loader with Table Pagination using REST API ?Trombone
M
6

If you want the spinner to overlay the rows, for example on paged tables, you can place the spinner overlay before the table with position: absolute. Just make sure to set position: relative on the parent container so the overlay does not extend it's parent size.

      <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>
Molehill answered 9/9, 2021 at 12:17 Comment(0)
B
5

Set showSpinner to true when you start requesting your data, and set it to false when you receive it (aka, in the subscribe of your service method)

ngOnInit() {
  this.showSpinner = true;
  this.annuairesService.getMedecins()
    .subscribe(res => {
      this.showSpinner = false;
      this.dataSource.data = res;
    });
}
Brownie answered 26/7, 2018 at 8:58 Comment(0)
L
4

Here is the most simple answer. In MatTable there is method *matNoDataRow sets row with this attribute when there is no data. So all you need is just reset your MatDataSource when recieving new data.

table.component.ts

dataSource!: MatTableDataSource<any>;

getData() {
   this.dataSource = new MatTableDataSource();
   this.loading = true;

   // Retrieving some data

   this.dataSource = new MatTableDataSource(yourNewData);
   this.loading = true;
}

table.component.html

<div class="mat-elevation-z8">
     <table #table mat-table [dataSource]="dataSource" matSort>
            <!-- table here ...-->
        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

        <!-- Row shown when there is no data. -->
        <tr class="mat-row" *matNoDataRow>
             <td *ngIf="loading==true" class="mat-cell-load" [attr.colspan]="colspan"><mat-progress-bar mode="indeterminate" class="table-progressbar"></mat-progress-bar></td>
             <td *ngIf="loading==false" class="mat-cell" [attr.colspan]="colspan">No data matching the filter "{{input.value}}"</td>
        </tr>
     </table>
</div>
Leavening answered 21/2, 2023 at 14:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.