I am building a DataTable on the fly, so I don't know the column content upfront. I would like to obtain a table for which each column width fits its content width. I tried to manipulate "autoWidth", but it resizes columns according to their number and to the table width, but all columns have the same width. How can I let each column adjust its width according to its own content?
2 Answers
You can use autoWidth: true along with columns.adjust() API method to adjust column width after you done adding data to the table.
3 Comments
Jar
"autoWidth": true worked for me, without using columns.adjust()
Fuseteam
appearently "autoWidth" is default to "true" i had to put it to "autoWidth": false and then use columns.adjust()
Gloria Chen
Yes, autoWidth here in the doc said
autoWidth default to true, so you don't have to set autoWidth: true;. You need to change it to false, then put columns.adjust() somewhere just after your data rendered in the Datatable.in my case,
i needed to fit / collapse table width down to the content.
autowidth: true wasn't enough. It also needed display:inline-table .
if i dont put inline-table, table expands to full width of page.
/* put 2 tables side by side */
.dataTables_wrapper {
display: inline-table;
/* below optional .*/
--width: auto;
--max-width: 45%;
}