ngx-datatable动态设置列宽 [英] Ngx-datatable set column width dynamically
问题描述
我将ngx-datatable的列宽存储在数据库中.我使用AJAX调用获取这些值.
如何为数据表设置这些值?
我尝试过的:
I'm storing the column widths of my ngx-datatable inside a database. I get these values using an AJAX call.
How can I set these values for the datatable?
What I've tried:
- 在
<ngx-datatable-column>
元素上设置[width]属性 - 将数据表注入为
@ViewChild(DatatableComponent) table: DatatableComponent;
并设置this.table.bodyComponent.columns[0].width = 500;
我已经尝试了使用this.table.recalculate();
和不使用this.table.recalculate();
的这些方法,但是似乎没有任何效果.
- setting the [width] property on the
<ngx-datatable-column>
element - injecting the datatable as
@ViewChild(DatatableComponent) table: DatatableComponent;
and settingthis.table.bodyComponent.columns[0].width = 500;
I've tried these methods with and withoutthis.table.recalculate();
, but nothing seems to work.
编辑
我正在将datatable-column
与header-template
和cell-template
一起使用.
EDIT
I'm using datatable-column
with header-template
and cell-template
.
推荐答案
虽然很简单,但我却能正常工作:
我将列的宽度值存储在用作字典的object
中.
问题是网格在我的ajax调用完成之前就已渲染,并且无法使网格重新绘制.
因此,我将字典对象的初始值设置为null
并将*ngIf
放置在网格上:<ngx-datatable *ngIf="colSizes">
I got it working, though it was very trivial:
I store the width values of the columns in an object
which acts as a dictionary.
The problem was that the grid was rendered before my ajax call has finished and could not make the grid to redraw itself.
So I set the initial value of my dictionary object to null
and put an *ngIf
on the grid: <ngx-datatable *ngIf="colSizes">
这样,仅在准备好使用字典的值之后才进行渲染.
This way the rendering happens only after the values of the dictionary are ready to be used.
这篇关于ngx-datatable动态设置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!