columns.adjust()
Recalculate the column widths.
Description
Like HTML tables, DataTables attempts to layout tables in an optimal format based on the data in the cells. As the data changes, it can sometimes be useful to recalculate this layout. Additionally DataTables makes extensive use of the dimensions of DOM elements in the table which scrolling is enabled (to align the columns) so if a table is hidden when initialised the height / width of the table elements will be unavailable.
This method is provided to have DataTables recalculate the columns sizes, based on the data in the table and the size applied to the columns (in the DOM, CSS or through the columns.width
parameter). Call it when the table becomes visible if hidden when initialised (for example in a tab) or when the data changes significantly.
Please note that DataTables 2.2 and newer make use of ResizeObserver
to perform this action automatically when the table container changes size. If you are using DataTables 2.2 and any browser released after 2020, you are unlikely to need to call this method.
For legacy browsers DataTables will automatically call this method on the window resize
event to keep the columns in sync with the re-flowed layout. In such browsers, if the content is changed and the window isn't resized, you will still need to call this method.
Type
columns.adjust()
Example
Adjust the column sizes of a newly shown table:
var table = new DataTable('#myTable');
$('#container').css('display', 'block');
table.columns.adjust().draw();
Related
The following options are directly related and may also be useful in your application development.