angular - 刷新 ngx-datatable 标头
问题描述
对于我正在构建的数据表,我需要使用动态标题(单击按钮时,将不同的数据加载到表中并且标题需要更改)。我正在使用它在软删除用户和普通用户之间切换(删除列更改为恢复列)。
更新行,并使用它,表的工作方式如下所述:
this.rows = [...this.rows];
更新我的专栏是另一回事。我已经尝试过使用相同的方式,this.columns = [...this.columns];
但这不会刷新表头。现在我拥有的是一个表,其中标题仅在后台刷新时才会更改,我似乎无法找到自己触发刷新的方法。我的表如下所示:
<ngx-datatable
[rows]="rows"
[columns]="columns"
[columnMode]="'flex'"
[sortType]="'single'"
[headerHeight]="50"
[footerHeight]="50"
[selected]="selected"
(select)='onSelect($event)'
[selectionType]="'checkbox'"
[selectAllRowsOnPage]="true"
[limit]="rowLimit"
[reorderable]="false"
[rowHeight]="'auto'" style="width: 100%">
</ngx-datatable>
这是我更新列的代码:
this.route.params.subscribe(params => {
this.state = params.state;
if (this.state === 'deleted') {
this.columns[columnIndex].name = 'Recover';
} else if (this.state === 'notactivated') {
this.columns[columnIndex].name = 'Delete';
}
解决方案
好吧,我又花了几个小时,但我想通了。这有点麻烦,但如果我将列 ID 设置为未定义,我可以强制刷新列。下面的代码在每次更新列标题时都有效并强制刷新(我称之为switchedColumn
):
// If there is a switchedColumn, remove the old column and add this as a new column to force a refresh
if (switchedColumn && switchedColumn.$$id) {
this.columns = this.columns.filter(c => c.$$id != switchedColumn.$$id);
switchedColumn.$$id = undefined;
this.columns = [...this.columns, switchedColumn]
}
推荐阅读
- mysql - 在 docker 中运行 MySQL 时出现“访问被拒绝”错误
- android - 具有不同尺寸图像的回收站视图显示项目
- web-deployment - 如何跟踪单个用户查看对象的时间
- c - 从C中的堆栈中释放内存
- python - 如何调用函数而不导致调用循环
- json - 来自 curl 输出的 JQ
- javascript - (d3) 单轴、可缩放时间线的动态分箱?
- linux - 查找 Ubuntu 驱动程序的源代码
- php - 数据库中没有添加任何内容(数据库连接问题?)
- automation - 尝试将自定义对象参数传递给 Cucumber 步骤定义时,总是出现“无法转换步骤的参数”错误?