Angular Material12:自定义Table排序按钮
Angular Material 12: Customize Table Sort Button
我有一个 Angular Material Table 排序:
table.component.html
:
<table mat-table [dataSource]="dataSource" matSort>
<th mat-header-cell mat-sort-header *matHeaderCellDef>
<span>Name</span>
</th>
...
</table>
这会以通常的 Material 样式在 header 单元格中显示一个排序按钮。
我需要更改排序按钮的样式(而不是功能)。
我怎样才能做到这一点?
我尝试创建自己的按钮,但我不知道如何访问 Material 排序按钮的功能。
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.-显示none“箭头”。为此,在您的 styles.css -所有应用程序通用的样式中 - 您可以编写
.mat-sort-header-ste,.mat-sort-header-arrow {
display:none!important
}
2.- 然后,如果您的排序称为 sort
,您需要使用“sort.active”和 sort.direction`
的值
硬代码,对于名为“name”的列可以是
@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>
您可以看到值是如何变化的
我有一个 Angular Material Table 排序:
table.component.html
:
<table mat-table [dataSource]="dataSource" matSort>
<th mat-header-cell mat-sort-header *matHeaderCellDef>
<span>Name</span>
</th>
...
</table>
这会以通常的 Material 样式在 header 单元格中显示一个排序按钮。
我需要更改排序按钮的样式(而不是功能)。 我怎样才能做到这一点? 我尝试创建自己的按钮,但我不知道如何访问 Material 排序按钮的功能。
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.-显示none“箭头”。为此,在您的 styles.css -所有应用程序通用的样式中 - 您可以编写
.mat-sort-header-ste,.mat-sort-header-arrow {
display:none!important
}
2.- 然后,如果您的排序称为 sort
,您需要使用“sort.active”和 sort.direction`
硬代码,对于名为“name”的列可以是
@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>
您可以看到值是如何变化的