【问题标题】:angular mat-table with dynamic columns and row headers for a data array具有数据数组的动态列和行标题的角度垫表
【发布时间】:2021-11-14 15:21:41
【问题描述】:

表格应显示在displayedColumns 中定义的数据。组列的行跨度为 2,行列显示 [rows.title],它是行标题,每次迭代都会显示。其余列显示有关 row.title 和 col 的数据如果有人可以帮助处理表格 html。

//column order
let displayedColumns = ['group','row','0','1','2'];

// tabledata object
let tableData = [{
  salesHist: [1, 2, 3, 4],
  sales: [1, 2, 3, 4],
  forecastHist: [1, 2, 3, 4],
  forecast: [1, 2, 3, 4],
  group: "000",
  recordCount: "1234",
  rows: [{
    key: "forecast",
    title: "Forecast"
  }, {
    key: "sales",
    title: "Sales"
  }, {
    key: "salesHist",
    title: "Sales History"
  }]
}];

//table html
<table
  mat-table
  matSort
  [dataSource]="table"
  multiTemplateDataRows
  class="w-100"
>
  <ng-container *ngFor="let group of table" matColumnDef="group">
    <th mat-header-cell *matHeaderCellDef mat-sort-header>
      {{ group }}
    </th>
    <td mat-cell *matCellDef="let row" [attr.rowspan]="4">
      {{ group }}
    </td>
  </ng-container>

  <ng-container matColumnDef="row">
    <th mat-header-cell *matHeaderCellDef></th>
    <td mat-cell *matCellDef="let row; let i = index">
      <span *ngFor="let rowTitle of row.rows">{{ rowTitle.title }}</span>
    </td>
  </ng-container>

  <ng-container
    *ngFor="let periodCol of monthsPeriod"
    matColumnDef="{{ periodCol.col }}"
  >
    <th mat-header-cell *matHeaderCellDef>
      {{ getTitle(periodCol) }}
    </th>
    <td class="sim-row-cell" mat-cell *matCellDef="let rowTitle">
      {{ getValue(rowTitle, periodCol) }}
    </td>
  </ng-container>
  <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
  <tr mat-row *matRowDef="let element; columns: displayedColumns"></tr>
</table>

【问题讨论】:

标签: angular angular-material mat-table


【解决方案1】:

我认为这可以为您提供一些解决问题的想法。看一看 组件.html

<app-loading *ngIf="isLoading$ | async"></lotus-app>

<div *ngIf="(isLoading$ | async) === false">
   <ng-container *ngIf="dataSource.data.length >= 1; else noData">
      <mat-table [dataSource]="dataSource" matSort>
         <ng-container *ngFor="let col of cols; let i = index" [matColumnDef]="col.key">
            <mat-header-cell *matHeaderCellDef mat-sort-header>
               {{cols[i]["display"]}}
            </mat-header-cell>
            <mat-cell *matCellDef="let element">
               <ng-container *ngIf="!col.config; else hasConfig">
                  {{element[col.key]}}
               </ng-container>
               <ng-template #hasConfig>
                  <ng-container *ngIf="col.config.isDate">
                     {{element[col.key] | date:'short'}}
                  </ng-container>
                  <ng-container *ngIf="col.config.isCurrency">
                     {{element[col.key] | currency}}
                  </ng-container>
               </ng-template>
            </mat-cell>
          </ng-container>
          <mat-header-row *matHeaderRowDef="keys"></mat-header-row>
          <mat-row *matRowDef="let row; columns: keys;"></mat-row>                                     
       </mat-table>
    </ng-container>
    <ng-template #noData>
       <h3>Ops...</h3>
       <p>There is no data...</p>
    </ng-template>
</div>

组件.ts

export class Component implements OnInit {
  cols = [
    { key: 'username', display: 'User Custom displayed name in column' },
    { key: 'salary', display: 'Some coin', config: { isCurrency: true } },
    { key: 'email', display: 'email' },
    { key: 'birthday', display: 'Birthday', config: { isDate: true } }
  ];
...
component logic..
// This is important to map all keys 
get keys() { return this.cols.map(({ key }) => key); } 

通过这种方式,您可以添加 N 列,并使用自定义名称显示 您只需在 att display 中设置可显示的名称,并使用 key att 获取服务器是否响应。

在模板中*matHeaderRowDef="keys" 将得到来自get keys() 的响应,但会在表格标题中显示显示 att 值

<ng-container *ngFor="let col of cols; let i = index" [matColumnDef]="col.key">
     <mat-header-cell *matHeaderCellDef mat-sort-header>
          {{cols[i]["display"]}}

...

继续编码\m/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    相关资源
    最近更新 更多