【问题标题】:Angular material - mat table - sort and filter do not work?角材料 - 垫表 - 排序和过滤不起作用?
【发布时间】:2018-06-20 09:45:06
【问题描述】:

我对角材料的垫子有疑问。 我无法让排序和过滤器工作。 (我想我完全喜欢文档..)

这是我的代码:

我的组件:

   listPatientsStay: PatientStay[] = [
        {lastName: 'lastname1', firstName: 'firstname1', sex: 'f', birthDate: '1990-01-01'},
        {lastName: 'lastname2', firstName: 'firstname2', sex: 'm', birthDate: '1990-01-01'},
        {lastName: 'lastname3', firstName: 'firstname3', sex: 'f', birthDate: '1990-01-01'}
      ];
    displayedColumns: string[] = ['lastName', 'firstName', 'sex', 'birthDate'];
    dataSource;
    @ViewChild(MatSort) sort: MatSort;

    ngOnInit() {
      this.dataSource = new MatTableDataSource<PatientStay>(this.listPatientsStay);
      this.dataSource.sort = this.sort;
    }

 applyFilter(filterValue: string) {
    console.log('Apply filter', filterValue);
    filterValue = filterValue.trim();
    filterValue = filterValue.toLowerCase();
    this.dataSource.filter = filterValue;
  }

我的模板:

<mat-form-field class="half-width">
          <input matInput (keyup)="applyFilter($event.target.value)"  placeholder="Filter">
</mat-form-field>

 <mat-table [dataSource]="dataSource" matSort>

        <ng-container matColumnDef="lastName">
          <mat-header-cell *matHeaderCellDef mat-sort-header> Lastname</mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.lastName}}</mat-cell>
        </ng-container>

        <ng-container matColumnDef="firstName">
          <mat-header-cell *matHeaderCellDef mat-sort-header> Firstname</mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.firstName}}</mat-cell>
        </ng-container>

        <ng-container matColumnDef="sex">
          <mat-header-cell *matHeaderCellDef mat-sort-header> Sex</mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.sex}}</mat-cell>
        </ng-container>

        <ng-container matColumnDef="birthDate">
          <mat-header-cell *matHeaderCellDef mat-sort-header> Birthdate</mat-header-cell>
          <mat-cell *matCellDef="let element"> {{element.birthDate | date: 'dd.MM.yyyy'}}</mat-cell>
        </ng-container>

        <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumns;"
                 (click)="onRowClicked(row)"></mat-row>

 </mat-table>

已导入: 垫表模块 MatSortModule

有 0 个错误,但它只是不过滤也不排序。

感谢您的帮助..

【问题讨论】:

  • 如果禁用排序功能,过滤器是否工作?
  • 不幸的是没有..
  • 如果以下答案之一回答了您的问题,您能否将问题标记为已回答?

标签: angular html-table angular-material


【解决方案1】:

这是一个简单的拼写错误:

参数是“this.listPatientsStay”,但属性名为“listPatientStay”。 ngOnInit 方法应如下所示:

ngOnInit() {
    this.dataSource = new MatTableDataSource<PatientStay>(this.listPatientStay);
    this.dataSource.sort = this.sort;
}

在 Stackblitz 上结帐:https://stackblitz.com/edit/angular-hrdv2z

一般来说,问题 Mat-table Sorting Demo not Working 解释了为什么必须包含 MatSortModule。

【讨论】:

  • 这根本不加载数据
  • 不,我只是在写这篇文章时犯了一个错误。我的代码没问题。我看到标签及其所有内容,仍然有排序和过滤的问题
  • @Helene 好的,让我们看看,Stackblitz sn-p 是否适合您?您发送的代码片段和修复程序按预期执行过滤技术。如果代码在您的项目中不起作用,我认为原因是项目中的其他地方,我需要更多信息来帮助您。
【解决方案2】:

纠正拼写错误。添加这个 --> this.dataSource = new MatTableDataSource(this.listPatientStay);

【讨论】:

    【解决方案3】:

    我发现了问题。 当我评论代码的分页部分时它可以工作(过滤和排序) 感谢那些试图帮助我的人

    我现在将解决分页问题;)

    【讨论】:

    猜你喜欢
    • 2019-01-28
    • 2021-08-03
    • 2018-06-22
    • 1970-01-01
    • 2019-07-19
    • 2018-11-07
    • 2019-11-22
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多