【发布时间】:2019-01-18 22:05:16
【问题描述】:
我目前正在学习 Ajden 在 PluralSight 上的“Angular Material”课程,但我正面临一些奇怪的行为,即与表格一起使用的排序无法完全正常工作。在课程中,显示了 3 列,第一列包含数字,当我尝试对该列进行排序时,没有任何反应,数字保持在显示的方式中。当我对第二列(文本列)进行排序时,排序工作正常,可以升序也可以降序。当我在对文本列进行排序后再次尝试对数字列进行排序时,排序将应用于数字列,但始终升序,从不降序。我的代码中是否缺少某些内容,或者这可能是角度材料中的错误?我正在使用最新版本的 Angular 和 Angular Material,因为我想以这种方式学习它。
这是我到目前为止与排序相关的代码:
数字排序
<mat-form-field>
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter"> </mat-form-field>
<table mat-table matSort [dataSource]="dataSource">
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef mat-sort-header>No.</th>
<td mat-cell *matCellDef="let note"> {{note.id}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="title">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Title</th>
<td mat-cell *matCellDef="let note"> {{note.title}} </td>
</ng-container>
<!-- Date Column -->
<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Date</th>
<td mat-cell *matCellDef="let note"> {{note.date | date: 'd LLLL yyyy'}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr> </table>
<mat-paginator [pageSize]="2" [pageSizeOptions]="[1, 2, 5]" showFirstLastButtons></mat-paginator>
将排序元素链接到数据源
@ViewChild(MatSort)
sort: MatSort;
ngAfterContentInit(): void {
this.dataSource = new MatTableDataSource<Note>(this.notes);
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
我还把我目前拥有的代码放在了一个 git 存储库中,可以在 here 找到它
【问题讨论】:
-
您的代码有问题。你能解决这些吗?您可以在浏览器控制台或终端窗口中检查错误。
-
您能说出您面临哪些问题吗?因为当我在命令窗口中执行 ng serve 时,它运行良好,并且我的浏览器控制台中没有任何错误。 ng build 也运行成功,没有任何错误。
-
您确定已将所有更改推送到 git 吗?尝试在 stackblitz 上运行:stackblitz.com/github/Cornelis83/ps-angular-material。以下是错误:
ERROR in ./src/app/app.component.ts Module not found: Error: Can't resolve './app.component.scss' in 'D:\tutorials\ps-angular-material\src\app' ERROR in ./src/app/contactmanager/contactmanager-app.component.ts Module not found: Error: Can't resolve './contactmanager-app.component.scss' in 'D:\tutorials\ps-angular-material\src\app\contactmanager' ERROR in ./src/app/contactmanager/components/... -
好吧,我实际上是使用 github 的上传功能来获取那里的代码,但我不知道 github 以这种方式拒绝空文件,并且一些 .scss 文件是空的。所以我解决了这个问题。
标签: angular sorting angular-material2