繁体   English   中英

Angular Material 12:自定义表格排序按钮

[英]Angular Material 12: Customize Table Sort Button

我有一个带排序的角度材料表:

table.component.html :

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

  <th mat-header-cell mat-sort-header *matHeaderCellDef>
    <span>Name</span>
  </th>

  ...
</table>

这会以通常的材料样式在标题单元格中显示一个排序按钮。

我需要更改排序按钮的样式(不是功能)。 我怎样才能做到这一点? 我尝试创建自己的按钮,但我不知道如何访问材料排序按钮的功能。

table.component.html :

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

  <th mat-header-cell *matHeaderCellDef>
    <span>Name</span>

    <!-- `style` contains some custom styling -->
    <!-- `(click)` should do the same as on the Material sort button -->
    <button mat-button mat-icon-button (click)=??? style=...>
      <mat-icon>sort</mat-icon>
    </button>
  </th>

  ...
</table>

我不知道这是否是最好的方法,但你可以

1.-使显示没有“箭头”。 为此,在你的styles.css - 所有应用程序共有的样式 - 你可以写

.mat-sort-header-ste,.mat-sort-header-arrow {
  display:none!important
}

2.- 那么,如果你的排序被称为sort ,你需要使用 "sort.active" 和 sort.direction` 的值

硬代码,对于名为“名称”的列可以是

@ViewChild(MatSort) sort: MatSort;
this.dataSource.sort = this.sort;

<th mat-header-cell *matHeaderCellDef mat-sort-header> 
{{sort && sort.active=="name"?
      sort.direction=='asc'?'Up':
      sort.direction=='desc'?'Down':
      null:null
}} Name </th>

您可以使用 [ngClass] 或其他方法

注意:如果你在组件的底部写

<pre>
{{sort?.active|json}} {{sort?.direction|json}}
</pre>

您可以看到值如何变化

一个丑陋的堆栈闪电战

暂无
暂无

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

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