typescript - name.replace 不是具有动态列的 mat 表中的函数
问题描述
我需要使用带有动态列的 mat 表,但出现此错误:
> ERROR TypeError: name.replace is not a function
> at MatColumnDef.set name [as name] (table.js:175)
> at updateProp (core.js:32189)
> at checkAndUpdateDirectiveInline (core.js:31867)
> at checkAndUpdateNodeInline (core.js:44367)
> at checkAndUpdateNode (core.js:44306)
> at debugCheckAndUpdateNode (core.js:45328)
> at debugCheckDirectivesFn (core.js:45271)
> at Object.eval [as updateDirectives] (TableComponent.html:3)
> at Object.debugUpdateDirectives [as updateDirectives] (core.js:45259)
> at checkAndUpdateView (core.js:44271)
在我的 ts 中,我宣布:
tableConfigurations = {
dataSource: [],
columns: [{
name: 'first'
},
{
name: 'second'
},
{
name: 'third'
},
{
name: 'fourth'
},
{
name: 'fifth'
}]
}
在html中我有:
<table mat-table [dataSource]="tableConfigurations.dataSource" class="mat-elevation-z8">
<ng-container [matColumnDef]="column" *ngFor="let column of tableConfigurations.columns">
<ng-container>
<th mat-header-cell *matHeaderCellDef> {{column.name}} </th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>
</ng-container>
<tr mat-header-row *matHeaderRowDef="tableConfigurations.columns"></tr>
<tr mat-row *matRowDef="let row; columns: tableConfigurations.columns;"></tr>
</table>
这是我拥有的当前代码的堆栈闪电战。正如您在控制台中看到的,存在相同的错误。我不明白我所缺少的。
解决方案
问题在
columns: [{ name: 'first' }, {name: 'second'}, ... ]
您可以采用两种方法:
列数组将变为字符串数组,如:
columns: ['first', 'second', ... ]
角度材料表显示列必须是字符串数组才能解析列。
- 添加一个变量,用于将列数组转换为字符串数组以正确显示它
displayedColumns: any[] = this.tableConfigurations.columns.map(col => col.name);
最后:
您的 html 将如下所示:
<table mat-table [dataSource]="tableConfigurations.dataSource" class="mat-elevation-z8">
<ng-container [matColumnDef]="column.name" *ngFor="let column of tableConfigurations.columns;">
<th mat-header-cell *matHeaderCellDef> {{column.name}}</th>
<td mat-cell *matCellDef="let element"> {{element[column.name]}}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table>
推荐阅读
- python - 在 Django 项目中为可重用应用程序创建基于类的 Celery 任务
- javascript - 如何仅使用 Javascript 动态创建带有上一个和下一个按钮的图像视频缩略图滑块?
- javascript - .modal-content 用于不同的模态(react-bootstrap)
- android - Android(Firestore):无法在 Firestore 中已创建的文档中添加、更新或设置(SetOptions.merge)字段
- sql - 转换失败的 varchar 值
- python - 如何使用 result.fit_report() 给出的值?
- vue.js - 在 Vue Good Table 中格式化输出行数据
- c# - 全球秒表 WPF - 如何/为什么这不起作用
- angular - 无法导航到同级路由器
- javascript - 让 Microsoft Azure Web 应用程序聊天机器人使用电子邮件或 Twilio 从预设用户的表/列表中主动联系用户