只使选定的列可排序,而不是在Angular材料表中使所有列可排序。

huangapple go评论103阅读模式
英文:

Only make selected columns as sortable instead of all columns in angular material table

问题

我正在使用Angular Material表格,以下是代码:

  1. <mat-table [dataSource]="dataSource" matSort>
  2. <!-- 表格列 -->
  3. <ng-container *ngFor="let column of displayedColumns; let i = index" [matColumnDef]="column">
  4. <th mat-header-cell *matHeaderCellDef [mat-sort-header]="sortableColumns.includes(column) ? column : ''" style="flex: 1">{{ headerLabels[i] }}</th>
  5. <td mat-cell *matCellDef="let element" style="flex: 1">
  6. <!-- 对特定列应用数字格式化 -->
  7. <ng-container *ngIf="shouldApplyNumberFormatting(column)">
  8. {{ element[column] | number }}
  9. </ng-container>
  10. <!-- 显示其他列内容 -->
  11. <ng-container *ngIf="!shouldApplyNumberFormatting(column)">
  12. {{ element[column] }}
  13. </ng-container>
  14. </td>
  15. </ng-container>
  16. <!-- 表格行 -->
  17. <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
  18. <tr mat-row *matRowDef="let row; columns: displayedColumns; let even = even" [class.mat-table-alternate-row]="even"></tr>
  19. </mat-table>

目前,它在所有列上都显示排序选项,但我希望只在某些列上显示排序选项,因此我决定声明一个用于列名称的数组,名为sortableColumns,其中包含需要排序选项的列,并应用以下代码:

  1. [mat-sort-header]="sortableColumns.includes(column) ? column : ''"

但仍然在所有列上显示排序选项。

以下是.ts文件中的排序绑定代码:

  1. constructor(private dashboardService: DashboardService, private decimalPipe: DecimalPipe) { }
  2. @ViewChild(MatPaginator) paginator!: MatPaginator;
  3. @ViewChild(MatSort) sort!: MatSort;
  4. ngAfterViewInit(): void {
  5. if (this.dataSource) {
  6. this.dataSource.paginator = this.paginator;
  7. this.dataSource.sort = this.sort;
  8. }
  9. }
  10. ngOnInit(): void {
  11. this.dashboardService.getDashboardData().subscribe((data: DashboardData[]) => {
  12. this.dataSource = new MatTableDataSource<DashboardData>(data);
  13. this.dataSource.paginator = this.paginator;
  14. this.dataSource.sort = this.sort;
  15. });
  16. }

希望这对你有所帮助!

英文:

I am using angular material table and here is code as follow :

  1. &lt;mat-table [dataSource]=&quot;dataSource&quot; matSort &gt;
  2. &lt;!-- Table columns --&gt;
  3. &lt;ng-container *ngFor=&quot;let column of displayedColumns; let i = index&quot; [matColumnDef]=&quot;column&quot;&gt;
  4. &lt;th mat-header-cell *matHeaderCellDef [mat-sort-header]=&quot;sortableColumns.includes(column) ? column : &#39;&#39;&quot; style=&quot;flex: 1&quot;&gt;{{ headerLabels[i] }}&lt;/th&gt;
  5. &lt;td mat-cell *matCellDef=&quot;let element&quot; style=&quot;flex: 1&quot;&gt;
  6. &lt;!-- Apply number formatting to specific columns --&gt;
  7. &lt;ng-container *ngIf=&quot;shouldApplyNumberFormatting(column)&quot;&gt;
  8. {{ element[column] | number }}
  9. &lt;/ng-container&gt;
  10. &lt;!-- Display other columns as is --&gt;
  11. &lt;ng-container *ngIf=&quot;!shouldApplyNumberFormatting(column)&quot;&gt;
  12. {{ element[column] }}
  13. &lt;/ng-container&gt;
  14. &lt;/td&gt;
  15. &lt;/ng-container&gt;
  16. &lt;!-- Table rows --&gt;
  17. &lt;tr mat-header-row *matHeaderRowDef=&quot;displayedColumns; sticky: true&quot;&gt;&lt;/tr&gt;
  18. &lt;tr mat-row *matRowDef=&quot;let row; columns: displayedColumns; let even = even&quot; [class.mat-table-alternate-row]=&quot;even&quot;&gt;&lt;/tr&gt;
  19. &lt;/mat-table&gt;

Its currently show sort option on all the columns but I want that on certain columns so I decided to declare an array for the column name as sortableColumns which I need the sorting option and apply

[mat-sort-header]=&quot;sortableColumns.includes(column) ? column : &#39;&#39;&quot;

but still its shows sorting option on all columns.

here is the .ts file code where sort is bind

  1. constructor(private dashboardService: DashboardService,private decimalPipe: DecimalPipe) { }
  2. @ViewChild(MatPaginator) paginator!: MatPaginator;
  3. @ViewChild(MatSort) sort!: MatSort;
  4. ngAfterViewInit(): void {
  5. if (this.dataSource) {
  6. this.dataSource.paginator = this.paginator;
  7. this.dataSource.sort = this.sort;
  8. }
  9. }
  10. ngOnInit(): void {
  11. this.dashboardService.getDashboardData().subscribe((data: DashboardData[]) =&gt; {
  12. this.dataSource = new MatTableDataSource&lt;DashboardData&gt;(data);
  13. this.dataSource.paginator = this.paginator;
  14. this.dataSource.sort = this.sort;
  15. });
  16. }

答案1

得分: 1

在mat-sort-header中有一个属性"disabled",请参阅文档

所以,我想你需要像这样使用:

  1. <th mat-header-cell *matHeaderCellDef
  2. [mat-sort-header]="column"
  3. [disabled]="!sortableColumns.includes(column)">
  4. ...
  5. </th>

注意:你不能在.html中动态应用一个指令。如果是属性样式,你可以将其绑定到"null",如果条件不成功,但这对于指令不起作用。

  1. <!-- 仅当是属性时 -->
  2. <div [attr.NameAtribute]="(condition) ? true : null"></div>
  3. <!-- 或者,我们可以使用 -->
  4. <div [class.myClass]="condition ? true : null"></div>
  5. <!-- 这个不起作用 -->
  6. <div [mat-sort-header]="(condition) ? true : null"></div>
英文:

You have a property "disabled" in mat-sort-header, see the docs

So, I imagine you need use some like

  1. &lt;th mat-header-cell *matHeaderCellDef
  2. [mat-sort-header]=&quot;column&quot;
  3. [disabled]=&quot;!sortableColumns.includes(column)&quot;&gt;
  4. ...
  5. &lt;/th&gt;

NOTE: you can not apply dinamically a directive in the .html. If is an attribute or class or style you can binding to "null" if the condition is not sucefully, but this not work with a directive.

  1. &lt;!--only if is an attribute--&gt;
  2. &lt;div [attr.NameAtribute]=&quot;(condicion)?true:null&quot;&gt;&lt;/div&gt;
  3. &lt;!--or, we can use--&gt;
  4. &lt;div [class.myClass]=&quot;condition?true:null&quot;&gt;&lt;/div&gt;
  5. &lt;!--this NOT work--&gt;
  6. &lt;div [mat-sort-header]=&quot;(condition)?true:null&quot;&gt;&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年7月13日 17:19:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定