【问题标题】:Set Material Sort on Condition - Angular Project根据条件设置材料排序 - Angular Project
【发布时间】:2020-08-28 13:10:02
【问题描述】:

我正在使用 Material 从事 Angular 9 项目。

我有一个支持排序(Mat-sort)的材料表。排序运行良好。我现在只想允许某些列按条件排序。有没有办法将mat-sort-header 属性设置为条件?

这是我的表格和列:

<table mat-table [dataSource]="dataSource" matSort (matSortChange)="setSortInRoute()">
    <ng-container *ngFor="let column of tableConfig.columns">
      <ng-container matColumnDef="{{ column.columnDef }}">
        <th mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.title }}</th>
        <td mat-cell *matCellDef="let row">
          {{ row[column.dataProperty[0]] }}
        </td>
      </ng-container>
    </ng-container>

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

我尝试将mat-sort-header 属性设置为一个条件,如下所示:[mat-sort-header]="sortColumn" 其中sortColumn 是一个布尔变量。但是,即使 var 为 false,列仍然具有排序功能。

我计划将条件设为来自tableConfig.columns 的布尔变量,它可能是tableConfig.columns.allowSort。

有没有办法将此属性设置为条件?如果没有,关于如何实现这一点的任何建议?

谢谢

【问题讨论】:

标签: angular angular-material angular9


【解决方案1】:

默认情况下,当单击列标题时,mat-table 将更改参考列进行排序。如果您打算避免这种情况,您可以使用mat-header-cell 指令将disabled 属性添加到th。

... ... <th mat-header-cell *matHeaderCellDef mat-sort-header [disabled]="column.sortingDisabled">{{ column.title }}</th> ... ...

【讨论】:

  • TY!这正是我一直在寻找的。没有意识到他们是残疾人的财产!
猜你喜欢
  • 2019-06-09
  • 2022-12-10
  • 2019-01-03
  • 2018-12-07
  • 2016-10-24
  • 2018-09-12
  • 2017-10-21
  • 1970-01-01
  • 2018-08-23
相关资源
最近更新 更多