【问题标题】:Angular Material Sorting a number assending doesn't workAngular Material对数字进行排序不起作用
【发布时间】: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


【解决方案1】:

在 mat-sort-header 中,您必须传递要排序的 json 键,例如 mat-sort-header="emp"

【讨论】:

  • 谢谢,正如我所说,我正在学习不同(较新)版本的 angular 和 angular 材料的课程,并且在课程中,作者没有明确命名要排序的属性和他的排序工作,即使命名不同。但是从 Angular 6 开始,出于某种原因(可能会阻止魔术代码),名称必须相等,否则排序不再起作用。在复查了复数的 cmets 后,其他人也提出了同样的问题,显然我第一次错过了。
猜你喜欢
  • 1970-01-01
  • 2019-07-24
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
相关资源
最近更新 更多