Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Datatable: Fit column width to content

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?

like image 709
Manu Avatar asked Oct 28 '16 16:10

Manu


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.

like image 132
Gyrocode.com Avatar answered Oct 26 '22 17:10

Gyrocode.com


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%;
    }
like image 32
bh_earth0 Avatar answered Oct 26 '22 19:10

bh_earth0