【问题标题】:Angular Material Table Cell Formatting角材料表单元格格式
【发布时间】:2019-04-04 17:02:37
【问题描述】:

我正在使用角度材料表来显示数据并动态绑定表头和表数据。 有没有办法动态格式化特定列的单元格内容?

例如,我如何格式化金额列的值右对齐?

我的代码如下:

<table mat-table [dataSource]="dataSource" class="" style="width: 100%;">

      <ng-container [matColumnDef]="col" *ngFor="let col of displayedColumns; let i = index">
        <th *matHeaderCellDef> {{displayedFields[i].name}}</th>
        <td mat-cell *matCellDef="let element"> {{ element[col] }} </td>
      </ng-container> 

      <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>

而我的数据是这样的

[
  {
    "REFERENCE_ID": "ENT201810637610",
    "PRODUCT_TYPE": "IMPS",
    "CUSTOMER_REFERENCE": "CUS12123",
    "BENEFICIARY_NAME": "arun",
    "DEBIT_ACCOUNT": "100002258062",
    "AMOUNT": 342234,
    "STAGE_CODE": "FULLFILMENT",
    "STATUS_CODE": "NEW"
  },
  {
    "REFERENCE_ID": "ENT201808820426",
    "PRODUCT_TYPE": "IMPS",
    "CUSTOMER_REFERENCE": "12121",
    "BENEFICIARY_NAME": "Arun",
    "DEBIT_ACCOUNT": "32423424",
    "AMOUNT": 700,
    "STAGE_CODE": "INITIATION",
    "STATUS_CODE": "NEW"
  }
]

【问题讨论】:

  • 澄清一下,您希望表格上的样式特定单元格具有文本对齐、文本颜色等属性?

标签: javascript angular angular-material angular-material-table


【解决方案1】:

最好的解决方案是在 css 中使用如下选择器,其中 'column_name' 是您在 'matColumnDef' 中提供的名称

.mat-column-'column_name'{
   //your custom css 
   text-align: right;
 }

如果您的列 'matColumnDef' 是 'amount'

.mat-column-amount{
   //your custom css 
   text-align: right;
 }

【讨论】:

    【解决方案2】:

    如果您想为 mat-table 上的单元格设置样式,您可以使用 ::ng-deep CSS 选择器定位内部的每个元素,如下所示:

    ::ng-deep th.mat-header-cell{
        width: 140px;
        min-width: 140px;
    }
    

    您还可以使用[ngClass] 根据以下条件将类应用于单元格:

     <ng-container matColumnDef="outcome">
      <th mat-header-cell *matHeaderCellDef mat-sort-header class="border"> Outcome </th>
      <td mat-cell *matCellDef="let element" class="font-weight-normal text-center text-capitalize"
      [ngClass]="[element.outcome == '' ? 'not-provided' : 'not-provided',
                element.outcome == 'stopped' ? 'provided-stoped' : 'not-provided']">{{element.outcome == '' ? "not provided" : "provided"}}</span> </td>
    </ng-container>
    

    您只需在 CSS 文件中定义类

    【讨论】:

    • [ngClass] 是为这个用例设计的,所以这是最好的答案
    【解决方案3】:

    在我们的工作项目中,我们使用了很多 mat-tables。我从来没有运气通过 ngFor-ing 在&lt;ng-container&gt; 上制作任何类型的真正定制的表格。几乎我们的每一个表都是通过为每一列单独定义每个&lt;ng-container&gt; 来构建的。

    <ng-container matColumnDef="code">
        <th mat-header-cell *matHeaderCellDef> Employee Code </th>
        <td mat-cell *matCellDef="let employee"> {{ employee.code }} </td>
    </ng-container>
    
    <ng-container matColumnDef="description">
        <th mat-header-cell *matHeaderCellDef> Status </th>
        <td mat-cell *matCellDef="let employee"> 
              {{ employee.status?.description }} 
        </td>
    </ng-container>
    
    <ng-container matColumnDef="salary">
        <th mat-header-cell *matHeaderCellDef> Salary </th>
        <td mat-cell *matCellDef="let employee"> {{ employee.salary | currency}} </td>
    </ng-container>
    

    这样做的好处是您可以使用所需的样式定义每一列,以及添加更多特定于属性的选项,例如管道和/或 elvis 运算符。

    【讨论】:

      【解决方案4】:

      您可以通过向&lt;td&gt; 元素添加类似[align]="expression ? 'right' : ''" 的内容来动态地将列对齐设置为右对齐,因此对于您的代码,这将如下所示:

      <table mat-table [dataSource]="dataSource" class="" style="width: 100%;">
      
            <ng-container [matColumnDef]="col" *ngFor="let col of displayedColumns; let i = index">
              <th *matHeaderCellDef> {{displayedFields[i].name}}</th>
              <td mat-cell *matCellDef="let element" [align]="col === 'AMOUNT' ? 'right' : ''"> {{ element[col] }} </td>
            </ng-container> 
      
            <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
            <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
          </table>
      

      【讨论】:

        【解决方案5】:

        我想要一个类似的东西,但在特定列上显示更复杂的样式。我正在使用循环来显示所有需要的列。

        也许这段代码 sn-p 有帮助

        我在 mat-h​​eader-cell 和 mat-cell 中使用了一个简单的 ngIf 来控制它。

        <table mat-table [dataSource]="dataSource" class="preview-table">
            <ng-container matColumnDef="{{column}}" *ngFor="let column of dcolumns">
        
                <th mat-header-cell *matHeaderCellDef>
                    <ng-container *ngIf="!(column === 'tagList')">
                        {{column}}
                    </ng-container>
                    <ng-container *ngIf="column === 'tagList'">
                        {{column}} h
                    </ng-container>
                </th>
                <td mat-cell *matCellDef="let element">
                    <ng-container *ngIf="!(column === 'tagList')">
                        {{element[column]}}
                    </ng-container>
                    <ng-container *ngIf="column === 'tagList'">
                        {{element[column]}} h
                    </ng-container>
                </td>
        
            </ng-container>
        
            <!-- other field as per syntax -->
        
        </table>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-12
          • 1970-01-01
          • 2022-01-03
          • 2015-04-23
          相关资源
          最近更新 更多