繁体   English   中英

角度材料表:排序不起作用

[英]Angular Material Table: Sorting not working

在我的 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';

我只是不明白为什么排序在第一种情况下有效,但在第二种情况下无效。 有人知道这里发生了什么吗?

确保displayColumns数组中的column_name

displayColumns = [' column_name '];

html容器,

ng-container matColumnDef=" column_name "

和 dataSource 对象的键,

数据源 = [ {" column_name ": "value"} ];

一切都完美匹配。

这也可能是一组特定数据不起作用而其他数据集起作用的原因。

你确定你的第二个表(排序不起作用的那个)没有用 *ngIf 包裹在一个 div 中吗? 因为这是一个常见的问题,当模板被渲染时,由于 *ngIf,matSort 是未定义的。 以下是修复方法: Angular 5 Material Data Table 排序不起作用

还有另一个可能的问题。 除了列名称必须与“mat-h​​eader-row *matHeaderRowDef”中的属性“matHeaderRowDef”匹配外,列名称还必须与 dataSource 属性中使用的内容类型的类字段/属性名称匹配。

例如

<mat-table [dataSource]="mySource" matSort>
        <!-- modelViewFieldNameOne has to match the class field/property name! -->
        <ng-container matColumnDef="modelViewFieldNameOne">
            <mat-header-cell  *matHeaderCellDef
                             mat-sort-header>just a column</mat-header-cell>
            <mat-cell *matCellDef="let tableItem">{{ tableItem.getterForMyField }}</mat-cell>
        </ng-container>


// typescript where 'mySource' is kept
mySource = new MatTableDataSource<MyModelViewType>();


// typescript of MyModelViewType
export class MyModelViewType{

   // this is important, it has to match the ng-container matColumnDef attribute!!!
   private modelViewFieldNameOne

   // this getter naming is not important for mat-table attributes
   public get getterForMyField(): string { 
     return this.modelViewFieldNameOne;
   }
}

我的 mat-table 排序很好,直到模型视图类型(即上例中的 MyModelViewType)被重构但只有字段/属性名称更改,getter() 名称被保留,相应的表列停止正确排序。 一旦属性与名称匹配,它就会再次工作。

我正在使用:@angluar/cdk 9.2.2

对于使用 API 调用加载的数据

有可能在数据加载到表之前设置了排序; 从而弄乱了排序功能。


在这种情况下,更改:

** code to insert data into the table **

this.dataSource2.sort = this.sort;

** code to insert data into the table **

setTimeout(() => this.dataSource2.sort = this.sort, 2000); // Delay it by 2 seconds.

this.sort在哪里:

@ViewChild(MatSort, { static: false }) sort: MatSort;

我知道当它认为我混合了数据类型时我的排序不起作用。 所以在下面的列表中,当它到达 587 时,它将停止对 Value 列进行排序。
在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM