gpt4 book ai didi

javascript - Angular material2动态生成列

转载 作者:太空狗 更新时间:2023-10-29 18:30:49 27 4
gpt4 key购买 nike

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

<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'
}
]

header 中的变量 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)

是否可以动态生成列?

编辑:

笨蛋 example

最佳答案

我认为你在将 cdkColumnDef 输入传递给指令时犯了错误

cdkColumnDef='column.columnDef'

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

如果我是你,我会这样写:

[cdkColumnDef]="column.columnDef"

或者您也可以使用以下选项:

cdkColumnDef="{{column.columnDef}}"

bind-cdkColumnDef="column.columnDef"

Plunker Example

关于javascript - Angular material2动态生成列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com