【发布时间】:2019-01-28 02:33:58
【问题描述】:
在我的 Angular 应用程序(使用 Angular Material)中,我有几个表格。
奇怪的是,在一种情况下,排序工作,而在另一种情况下,它却没有。
这是有效的表格:
<table mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef> ID </th>
<td mat-cell *matCellDef="let row"> {{row.id}} </td>
</ng-container>
<ng-container matColumnDef="firstName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> First Name </th>
<td mat-cell *matCellDef="let row"> {{row.firstName}} </td>
</ng-container>
<ng-container matColumnDef="lastName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Last Name </th>
<td mat-cell *matCellDef="let row"> {{row.lastName}} </td>
</ng-container>
<ng-container matColumnDef="viewProfile">
<th mat-header-cell *matHeaderCellDef class="viewProfile"> Profile </th>
<td mat-cell *matCellDef="let row" class="viewProfile">
<button mat-icon-button (click)="openProfile(row.id)">
<mat-icon aria-label="icon-button with a page-view icon">pageview</mat-icon>
</button>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
...这是不起作用的表:
<table class="table2" mat-table [dataSource]="dataSource2" matSort>
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Project </th>
<td mat-cell *matCellDef="let row"> {{row.name}} </td>
</ng-container>
<ng-container matColumnDef="role">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Role </th>
<td mat-cell *matCellDef="let row"> {{row.role}} </td>
</ng-container>
<ng-container matColumnDef="beginning">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Beginning </th>
<td mat-cell *matCellDef="let row"> {{row.beginning | date : "mediumDate"}} </td>
</ng-container>
<ng-container matColumnDef="end">
<th mat-header-cell *matHeaderCellDef mat-sort-header> End </th>
<td mat-cell *matCellDef="let row"> {{row.end | date : "mediumDate"}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns2"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns2;"></tr>
</table>
如您所见,在这两种情况下,我都使用“matSort”(在table 标记中)和“mat-sort-header”(用于应该可排序的列)。
此外,在每种情况下,我都会在 component.ts 文件中进行相同的导入:
import { MatTableDataSource, MatPaginator, MatSort, MatDialog } from '@angular/material';
我只是不明白为什么排序在第一种情况下有效,但在第二种情况下无效。 有人知道这里发生了什么吗?
【问题讨论】:
-
您使用的是哪个版本的 Angular Material?
-
你的数据源支持排序吗?你能发布 plunkr 吗?
-
我正在使用 Angular Material 6
-
第二种情况下的数据源除了字符串之外还包含两个日期...这可能是原因吗?
标签: angular sorting html-table angular-material