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

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

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

问题

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

<mat-table [dataSource]="dataSource" matSort>
    <!-- 表格列 -->
    <ng-container *ngFor="let column of displayedColumns; let i = index" [matColumnDef]="column">
        <th mat-header-cell *matHeaderCellDef [mat-sort-header]="sortableColumns.includes(column) ? column : ''" style="flex: 1">{{ headerLabels[i] }}</th>

        <td mat-cell *matCellDef="let element" style="flex: 1">
            <!-- 对特定列应用数字格式化 -->
            <ng-container *ngIf="shouldApplyNumberFormatting(column)">
                {{ element[column] | number }}
            </ng-container>
            <!-- 显示其他列内容 -->
            <ng-container *ngIf="!shouldApplyNumberFormatting(column)">
                {{ element[column] }}
            </ng-container>
        </td>
    </ng-container>

    <!-- 表格行 -->
    <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns; let even = even" [class.mat-table-alternate-row]="even"></tr>
</mat-table>

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

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

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

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

constructor(private dashboardService: DashboardService, private decimalPipe: DecimalPipe) { }

@ViewChild(MatPaginator) paginator!: MatPaginator;
@ViewChild(MatSort) sort!: MatSort;

ngAfterViewInit(): void {
    if (this.dataSource) {
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
    }
}

ngOnInit(): void {
    this.dashboardService.getDashboardData().subscribe((data: DashboardData[]) => {

        this.dataSource = new MatTableDataSource<DashboardData>(data);
        this.dataSource.paginator = this.paginator;
        this.dataSource.sort = this.sort;
    });
}

希望这对你有所帮助!

英文:

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

&lt;mat-table [dataSource]=&quot;dataSource&quot; matSort &gt;
    &lt;!-- Table columns --&gt;
    &lt;ng-container *ngFor=&quot;let column of displayedColumns; let i = index&quot; [matColumnDef]=&quot;column&quot;&gt;
      &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;

      &lt;td mat-cell *matCellDef=&quot;let element&quot; style=&quot;flex: 1&quot;&gt;
        &lt;!-- Apply number formatting to specific columns --&gt;
        &lt;ng-container *ngIf=&quot;shouldApplyNumberFormatting(column)&quot;&gt;
          {{ element[column] | number }}
        &lt;/ng-container&gt;
        &lt;!-- Display other columns as is --&gt;
        &lt;ng-container *ngIf=&quot;!shouldApplyNumberFormatting(column)&quot;&gt;
          {{ element[column] }}
        &lt;/ng-container&gt;
      &lt;/td&gt;
    &lt;/ng-container&gt;
  
    &lt;!-- Table rows --&gt;
    &lt;tr mat-header-row *matHeaderRowDef=&quot;displayedColumns; sticky: true&quot;&gt;&lt;/tr&gt;
    &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;
  &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

constructor(private dashboardService: DashboardService,private decimalPipe: DecimalPipe) { }

  @ViewChild(MatPaginator) paginator!: MatPaginator;
  @ViewChild(MatSort) sort!: MatSort;

  ngAfterViewInit(): void {
    if (this.dataSource) {
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
    }
  }

  ngOnInit(): void {
    this.dashboardService.getDashboardData().subscribe((data: DashboardData[]) =&gt; {
      
      this.dataSource = new MatTableDataSource&lt;DashboardData&gt;(data);
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
    });
  }

答案1

得分: 1

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

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

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

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

<!-- 仅当是属性时 -->
<div [attr.NameAtribute]="(condition) ? true : null"></div>
<!-- 或者,我们可以使用 -->
<div [class.myClass]="condition ? true : null"></div>

<!-- 这个不起作用 -->
<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

&lt;th mat-header-cell *matHeaderCellDef 
   [mat-sort-header]=&quot;column&quot; 
   [disabled]=&quot;!sortableColumns.includes(column)&quot;&gt;
...
&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.

&lt;!--only if is an attribute--&gt;
&lt;div [attr.NameAtribute]=&quot;(condicion)?true:null&quot;&gt;&lt;/div&gt;
&lt;!--or, we can use--&gt;
&lt;div [class.myClass]=&quot;condition?true:null&quot;&gt;&lt;/div&gt;

&lt;!--this NOT work--&gt;
&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:

确定