我正在使用此代码
<mat-table #table [dataSource]="dataSource" matSort >
<ng-container matColumnDef="tache">
<mat-header-cell *matHeaderCellDef mat-sort-header> tâche </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.tache}} </mat-cell>
</ng-container>
<ng-container matColumnDef="outil">
<mat-header-cell *matHeaderCellDef mat-sort-header> outil </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.outil}} </mat-cell>
</ng-container> <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selection.toggle(row)">
</mat-row>
</mat-table>
Run Code Online (Sandbox Code Playgroud)
那么,如何在数据表中显示空消息"No Record found".
Ren*_*.N. 32
使用Angular Material 10 或更高版本如果要在数据与过滤器不匹配时显示消息,可以使用 *matNoDataRow 指令。
<tr class="mat-row" *matNoDataRow>
<td class="mat-cell" colspan="9999">
No data matching the filter
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
mah*_*val 12
就像bug在说,您可以使用*ngIf
。在这里比较这两个表:
https://stackblitz.com/edit/angular-w9ckf8
<mat-toolbar color="primary">My empty table</mat-toolbar>
<mat-table #table [dataSource]="dataSourceEmpty" matSort *ngIf="dataSourceEmpty.length > 0">
<ng-container matColumnDef="Name">
<mat-header-cell *matHeaderCellDef mat-sort-header>Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<ng-container matColumnDef="Age">
<mat-header-cell *matHeaderCellDef mat-sort-header>Age </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.age}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="row.toggle(row)">
</mat-row>
</mat-table>
<div *ngIf="dataSourceEmpty.length === 0">No records found</div>
<hr>
<mat-toolbar color="primary">My full table</mat-toolbar>
<mat-table #table [dataSource]="dataSource" matSort *ngIf="dataSource.length > 0">
<ng-container matColumnDef="Name">
<mat-header-cell *matHeaderCellDef mat-sort-header>Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<ng-container matColumnDef="Age">
<mat-header-cell *matHeaderCellDef mat-sort-header>Age </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.age}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="row.toggle(row)">
</mat-row>
</mat-table>
<div *ngIf="dataSource.length === 0">No data</div>
Run Code Online (Sandbox Code Playgroud)
TS与数据:
displayedColumns = ['Name', 'Age']
dataSource = [{name:'Sara',age:17}, {name: 'John', age: 20}]
dataSourceEmpty = []
Run Code Online (Sandbox Code Playgroud)
sad*_*287 11
如果是console.log dataSource,您将看到以下内容: dataSource示例
dataSource本身不是数组,而是dataSource.data.dataSource实际上是一个具有属性数据的类,其中包含传递给MatTableDataSource的内容(https://github.com/angular/material2/blob/master/src/lib/table/table-data-source.ts)因此,这是您应该用于*ngIf语句的内容.
<div *ngIf="dataSource.data.length === 0">
No Records Found!
</div>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
小智 7
<mat-footer-row *matFooterRowDef="['noData']" [ngClass]="{'hide':!(listData!=null && listData.filteredData.length==0)}"></mat-footer-row>
Run Code Online (Sandbox Code Playgroud)
通过这样做,我能够解决问题。
hide
是一个自定义的css
.hide{
display:none;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您可以通过以下方式将其放在页脚行中:
列定义:
<ng-container matColumnDef="noRecord">
<td mat-footer-cell *matFooterCellDef>No records found.</td>
</ng-container>
Run Code Online (Sandbox Code Playgroud)
页脚行定义:
<ng-template [ngIf]="dataSource.data.length === 0">
<tr mat-footer-row *matFooterRowDef="['noRecord']"></tr>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
有两种方法可以在html中显示错误消息
使用If方法的第一种方法
<div *ngIf="dataSource.length">
<mat-table #table [dataSource]="dataSource" matSort >
<ng-container matColumnDef="tache">
<mat-header-cell *matHeaderCellDef mat-sort-header> tâche </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.tache}} </mat-cell>
</ng-container>
<ng-container matColumnDef="outil">
<mat-header-cell *matHeaderCellDef mat-sort-header> outil </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.outil}} </mat-cell>
</ng-container> <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
<div *ngIf="!dataSource.length">
No Record found
</div>
Run Code Online (Sandbox Code Playgroud)
使用if else的第二种方法
<div *ngIf="dataSource.length; else noRecord">
<mat-table #table [dataSource]="dataSource" matSort >
<ng-container matColumnDef="tache">
<mat-header-cell *matHeaderCellDef mat-sort-header> tâche </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.tache}} </mat-cell>
</ng-container>
<ng-container matColumnDef="outil">
<mat-header-cell *matHeaderCellDef mat-sort-header> outil </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.outil}} </mat-cell>
</ng-container> <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="selection.toggle(row)">
</mat-row>
</mat-table>
</div>
<ng-template #noRecord>
<div>
No Record found
</div>
</ng-template>
Run Code Online (Sandbox Code Playgroud)
在你的component-name.component.html
:
<div class="mat-elevation-z8">
<mat-table [dataSource]="listData" matSort>
<ng-container matColumnDef="fullName">
<mat-header-cell *matHeaderCellDef mat-sort-header>Full Name</mat-header-cell>
<mat-cell *matCellDef="let element">{{element.fullName}}</mat-cell>
</ng-container>
...
...
<ng-container matColumnDef="noData">
<mat-footer-cell *matFooterCellDef colspan="6">
No data.
</mat-footer-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<mat-footer-row *matFooterRowDef="['noData']" [ngClass]="{'hide':!(listData!=null && listData.data.length==0)}"></mat-footer-row>
</mat-table>
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" [pageSize]="5" showFirstLastButtons></mat-paginator>
</div>
Run Code Online (Sandbox Code Playgroud)
在您style.scss
或component-name.component.scss
定义的.hide
班级中
.hide { display: none; }
Run Code Online (Sandbox Code Playgroud)
就这样 :)
步骤#0
在 ts
dataSource: any = new MatTableDataSource()
Run Code Online (Sandbox Code Playgroud)
步骤1
<table [dataSource]="dataSource">
<ng-container matColumnDef="nodata">
<td mat-footer-row *matFooterCellDef [colSpan]="displayedColumns.length"
style="text-align: center;">No Data Available</td>
</ng-container>
<tr mat-footer-row
[hidden]="dataSource.data.length >0"
*matFooterRowDef="['nodata']">
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28621 次 |
最近记录: |