8

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 2

8

You can use autoWidth: true along with columns.adjust() API method to adjust column width after you done adding data to the table.

Sign up to request clarification or add additional context in comments.

3 Comments

"autoWidth": true worked for me, without using columns.adjust()
appearently "autoWidth" is default to "true" i had to put it to "autoWidth": false and then use columns.adjust()
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.
1

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%;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.