Angular material2动态生成列

时间:2017-08-11 10:28:51

标签: javascript json angular angular-material2

我正在使用角度素材ng-table和wanto动态生成列。 以下示例包含html模板。这是“静态”定义的表模板的工作示例。我想用注释中的代码替换所有columnd定义。

<md-table #table [dataSource]="dataSource">
<ng-container cdkColumnDef="userId">
    <md-header-cell *cdkHeaderCellDef> ID </md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row['userId']}} </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="progress">
    <md-header-cell *cdkHeaderCellDef> Progress </md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row['progress']}}% </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="userName">
    <md-header-cell *cdkHeaderCellDef> Name </md-header-cell>
    <md-cell *cdkCellDef="let row"> {{row['userName']}} </md-cell>
  </ng-container>

  <ng-container cdkColumnDef="color">
    <md-header-cell *cdkHeaderCellDef>Color</md-header-cell>
    <md-cell *cdkCellDef="let row" [style.color]="row.color"> {{row['color']}} </md-cell>
  </ng-container>

  <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
  <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>

<!-- <ng-container
  *ngFor='let column of tableOptions.columnDefs'
  cdkColumnDef='column.columnDef'>
  <md-header-cell *cdkHeaderCellDef> {{column.headName}} </md-header-cell>
  <md-cell *cdkCellDef="let row"> {{row[column.columnDef]}} </md-cell>
</ng-container> -->

columnDefs json看起来像这样:

columnDefs: [
    {
      columnDef: 'userId',
      headName: 'ID'
    },
    {
      columnDef: 'userName',
      headName: 'Name'
    },
    {
      columnDef: 'progress',
      headName: 'Progress'
    },
    {
      columnDef: 'color',
      headName: 'Color'
    }
  ]

标头中的变量displayedColumns为数组["userId", "userName", "progress", "color"],其生成基于columnDefs json

当我尝试使用* ngFor时,我收到此错误:

    TableComponent.html:24 
ERROR Error: cdk-table: Could not find column with id "userId".
    at getTableUnknownColumnError (cdk.es5.js:1780)
    at cdk.es5.js:2111
    at Array.map (<anonymous>)
    at MdTable.webpackJsonp.../../../cdk/@angular/cdk.es5.js.CdkTable._getHeaderCellTemplatesForRow (cdk.es5.js:2108)
    at MdTable.webpackJsonp.../../../cdk/@angular/cdk.es5.js.CdkTable._renderHeaderRow (cdk.es5.js:2016)
    at SafeSubscriber._next (cdk.es5.js:1952)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
    at SafeSubscriber.webpackJsonp.../../../../rxjs/Subscriber.js.SafeSubscriber.next (Subscriber.js:185)
    at Subscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber._next (Subscriber.js:125)
    at Subscriber.webpackJsonp.../../../../rxjs/Subscriber.js.Subscriber.next (Subscriber.js:89)

是否可以动态生成列?

编辑:

Plunker example

1 个答案:

答案 0 :(得分:2)

我认为在将cdkColumnDef输入传递给指令

时你犯了错误
cdkColumnDef='column.columnDef'

这意味着cdkColumnDef会将'column.columnDef'字符串作为值。

如果我是你,我会把它写成:

[cdkColumnDef]="column.columnDef"

或以下选项也适用于您:

cdkColumnDef="{{column.columnDef}}"

bind-cdkColumnDef="column.columnDef"

Plunker Example