ngx-datatable动态设置列宽 [英] Ngx-datatable set column width dynamically

查看:587
本文介绍了ngx-datatable动态设置列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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:

  1. <ngx-datatable-column>元素上设置[width]属性
  2. 将数据表注入为@ViewChild(DatatableComponent) table: DatatableComponent;并设置this.table.bodyComponent.columns[0].width = 500;

    我已经尝试了使用this.table.recalculate();和不使用this.table.recalculate();的这些方法,但是似乎没有任何效果.
  1. setting the [width] property on the <ngx-datatable-column> element
  2. injecting the datatable as @ViewChild(DatatableComponent) table: DatatableComponent; and setting this.table.bodyComponent.columns[0].width = 500;

    I've tried these methods with and without this.table.recalculate();, but nothing seems to work.


编辑
我正在将datatable-columnheader-templatecell-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆