Ngx-datatable set column width dynamically

flow3r picture flow3r · Jan 12, 2018 · Viewed 39.1k times · Source

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.

Answer

Ren&#233; picture René · Jul 30, 2018

If you use the solution of Hoang Duc Nguyen, you can also store the width in the columns:

 columns = [
    { name: 'Name', prop: 'name', width: 250},
    { name: 'Gender', prop: 'gender'},
    { name: 'Company', prop: 'company', sortable: false }
  ];