如何在数据表角度材料中显示空消息,如果没有找到数据

a.e*_*mma 17 angular

我正在使用此代码

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

  • 您可以使用以下命令获得更好的 colspan:`&lt;td [colSpan]="displayedColumns.length"&gt;` (7认同)
  • 如果使用 `&lt;mat-table&gt;` 而不是 `&lt;table&gt;`,则只需将 `&lt;div *matNoDataRow&gt;No data&lt;/div&gt;` 放入 `&lt;mat-table&gt;` 内即可。尽管它仍然无助于隐藏“&lt;mat-paginator&gt;”(如果你有的话) (2认同)

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)

  • 该示例依赖于一个简单的数据源,即 JS 数组。当使用 Angular 在 @angular/cdk/collections/typings/data-source.d.ts 中提供的抽象类 DataSource&lt;T&gt; 的实例时,您有什么建议? (2认同)

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)

希望这可以帮助!

  • 以防万一有人使用过滤器,那么您需要检查“data.filteredData.length === 0” (2认同)

小智 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)

  • 我也尝试过这个解决方案,但它对我不起作用。当我删除一个对象并且表变空时,该行不会显示。当我重新加载页面时,它会显示出来,但是在添加对象时,该行不会消失。这很奇怪。任何人都可以为此提供一个工作示例吗? (2认同)

Sha*_*ram 5

有两种方法可以在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)


Mil*_*vic 5

在你的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.scsscomponent-name.component.scss定义的.hide班级中

.hide { display: none; }
Run Code Online (Sandbox Code Playgroud)

就这样 :)


Moh*_*jaz 5

步骤#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)