How can I set the width of a column, on a table that is already initiated?
How can I set the width of a column, on a table that is already initiated?
Hi all!
I want to set the width of my first column in my table, to make sure it is an exact width. For this support case here, I have just 100px as my example.
The table has already been inititiated, so it has to be set afterwards. The table has a fixedheader, and the first column is also fixed.
Below is the code that I use, and while it does change the sWidth property in the Settings, I can't get it to change the table at all. Any suggestions?
//Define my new width
let newWidth = 100;
//Set the width of the first column in the table settings
dataTableInstance.dataTableInstance.settings()[0].aoColumns[0].sWidth = newWidth + 'px';
//Redraw the table
dataTableInstance.columns().render();
dataTableInstance.columns.adjust().draw();
Answers
You can't, sorry. There is no public API that provides that ability in DataTables.
What I would encorage you to do is put the DataTable in a
div
container and change the width of that container. When you change the width of the container, make sure you callcolumns.adjust()
to tell DataTables about the change.Allan