Search code examples
javascriptangulartypescriptngx-datatable

Ngx-datatable set column width dynamically


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. 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.


EDIT
I'm using datatable-column with header-template and cell-template.


Solution

  • 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.