【问题标题】:How to use conditional formatting of Angular Material Table values如何使用 Angular Material Table 值的条件格式
【发布时间】:2021-10-15 09:58:27
【问题描述】:

我有一个工作正常的垫表,所有值都以 DB 返回的格式精确显示

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

    <ng-container *ngFor="let column of displayedColumns" matColumnDef="{{column.name}}">
      <mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.viewValue }}</mat-header-cell>
      <mat-cell *matCellDef="let item">{{item[column.name]}}</mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumnsKeys; sticky: true" ></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumnsKeys;"></mat-row>

  </mat-table>

每个项目的数据结构看起来像这样。用户可以从中选择从数据库返回的 300 多列,所以我不想硬编码表定义,而是有一个 JSON 结构来定义列及其值

 export interface SearchFields {
    name: string;
    viewValue: string;
    fieldType?: string;
    dispFormat?: string;
  }

现在我想使用 dispFormat 有条件地格式化一些值。 dispFormat 的可能值为''(留空)currencynumber 我不确定如何实现货币或数字管道,或者没有基于列定义的格式。我尝试添加不正确的 *ngIf


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

    <ng-container *ngFor="let column of displayedColumns" matColumnDef="{{column.name}}">
      <mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.viewValue }}</mat-header-cell>
      <ng-template #currValue *ngIf="column.dispFormat=='currency'" ><mat-cell *matCellDef="let item" >{{item[column.name] | currency:'USD':true:'2.2-4'}}</mat-cell></ng-template>
      <ng-template #noFormatValue *ngIf="column.dispFormat==''" > <mat-cell *matCellDef="let item" >{{item[column.name]}}</mat-cell></ng-template>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumnsKeys; sticky: true" ></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumnsKeys;"></mat-row>

  </mat-table>

但是由于混合 *ngIf 和 *matCellDef 会产生错误

ERROR TypeError: Cannot read property 'template' of undefined

如何根据条件格式化值?

编辑 - 根据要求,添加示例数据
示例数据源为:

[{name: 'Company A',
accountspayable: 172286000,
accumulatedamortization: null,
beta: 0.5718,
bookvalue: 15.128,
buy: 0},
{name: 'Company B',
accountspayable: 112676000,
accumulatedamortization: 1689965,
beta: 0.5,
bookvalue: 27.5,
buy: 1},
{name: 'Company C',
accountspayable: 126546000,
accumulatedamortization: 1889965,
beta: 0.168,
bookvalue: 10,
buy: 5}
]

列定义为:

[
{ name: 'name', viewValue: 'Name',dispFormat: '' },
{ name: 'accountspayable', viewValue: 'Accounts Payable', dispFormat: 'currency' },
{ name: 'accumulatedamortization', viewValue: 'Accumulated Amortization', dispFormat: 'currency' },
{ name: 'beta', viewValue: 'Beta', dispFormat: 'number' },
{ name: 'bookvalue', viewValue: 'Book Value', dispFormat: 'currency' },
{ name: 'buy', viewValue: 'Buy', dispFormat: 'number' }
]

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    我找到了答案,就是把 ngIf 放到 mat-cell 里面

      <mat-table #table [dataSource]="queryResults" matSort >
    
        <ng-container *ngFor="let column of displayedColumns" matColumnDef="{{column.name}}">
          <mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.viewValue }}</mat-header-cell>
          <mat-cell *matCellDef="let item">
            <div *ngIf="column.dispFormat=='currency'">
              {{item[column.name] | currency:'USD':true:'2.0-4'}}
            </div>
            <div *ngIf="column.dispFormat=='number'">
              {{item[column.name] | number : '2.0-4'}}
            </div>
            <div *ngIf="column.dispFormat==''">
              {{item[column.name]}}
            </div>
          </mat-cell>
        </ng-container>
    
        <mat-header-row *matHeaderRowDef="displayedColumnsKeys; sticky: true" ></mat-header-row>
        <mat-row *matRowDef="let row; columns: displayedColumnsKeys;"></mat-row>
    
      </mat-table>
    
    

    【讨论】:

    • 正如你所说,你不能在同一个标​​签中有两个“结构指令”(例如 *matHeaderCellDef 和 *ngIf)。如果您不想创建 HTML 元素。您可以使用ng-container(或在您制作时将一个结构指令移动到另一个元素)
    【解决方案2】:

    您可能想尝试不更改 mat 表语法:

          <mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.viewValue }}</mat-header-cell>
          <ng-template #currValue *ngIf="column.dispFormat=='currency'" ><mat-cell *matCellDef="let item" >{{item[column.name] | currency:'USD':true:'2.2-4'}}</mat-cell></ng-template>
          <ng-template #noFormatValue *ngIf="column.dispFormat==''" > <mat-cell *matCellDef="let item" >{{item[column.name]}}</mat-cell></ng-template>
        </ng-container>```
    
    could be changed to
    
    ```<ng-container *ngFor="let column of displayedColumns" matColumnDef="{{column.name}}">
          <mat-header-cell *matHeaderCellDef mat-sort-header>{{ column.viewValue }}</mat-header-cell>
          <mat-cell *matCellDef="let item" >
            <span *ngIf="column.dispFormat=='currency'">{{item[column.name] | currency:'USD':true:'2.2-4'}}</span>
            <span *ngIf="column.dispFormat==''">{{item[column.name]}}</span>
          </mat-cell>
       </ng-container>
    

    【讨论】:

    • 谢谢,但这不起作用。首先它由于错误Property 'item' does not exist... 而无法编译,所以我在&lt;span&gt; 中添加了mat-cell,然后我仍然收到运行时错误Cannot read property 'template' of undefined
    • 能否附上dataSource的结果截图
    • 我已经添加了示例数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2019-09-17
    • 1970-01-01
    • 2021-06-18
    • 2022-12-18
    • 2021-06-01
    相关资源
    最近更新 更多