Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto resizing of columns width datatable

The datatable column are not auto resizing. Here is my code

var oTable =$('#test').dataTable( {
        "bJQueryUI": true,
        "aaData": aDataSet,
        "sPaginationType": "full_numbers",
        "oTableTools": {
        "aButtons": [ {"sExtends": "csv" , "sButtonText": "Save as CSV"}],
        "sSwfPath": "js/jquery/copy_csv_xls.swf"
    },
    "bAutoWidth" : true,
    "sDom": '<"H"lCf>t"H"<"F"iTp>',
    "aoColumnDefs": [
        { "bVisible": true, "aTargets": [ 11 ] }
    ],
    "aoColumns": [
        { "sTitle": "column1" },
        { "sTitle": "column1" },
        { "sTitle": "column1" },
        { "sTitle": "column1"},
        { "sTitle": "column1"},
        { "sTitle": "column1" },
        { "sTitle": "column1" },
        { "sTitle": "column1" },
        { "sTitle": "column1"},
        { "sTitle": "column1 By"},
        { "sTitle": "column1 Date"}
    ]
    } );
oTable.fnAdjustColumnSizing();
});

I want all the columns to auto resize at least based on their header value.

like image 740
af_khan Avatar asked Jan 24 '14 10:01

af_khan


People also ask

What is autoWidth in DataTable?

The autoWidth option is used to specify whether the automatic calculation of the column width in the DataTable is enabled or not. This calculation takes a little time and may be disabled when the column widths are explicitly passed using the columns.

How do you set column width in lightning DataTable?

Set to 'fixed' for columns with equal widths. Set to 'auto' for column widths that are based on the width of the column content and the table width. The default is 'fixed'.

How do you resize a data table?

Select the table, then select Table Design > Resize Table.


2 Answers

You just do it like if it was a "normal" <table> :

th, td {
    white-space: nowrap;
}

see fidle -> http://jsfiddle.net/YrWG5/ with some extreme long header / content.

like image 148
davidkonrad Avatar answered Sep 22 '22 13:09

davidkonrad


You can also use datatable's Fluid column width feature. This will help auto-resize and add a scroll both on the X and Y axis should you have more columns to display

$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        columnDefs: [
            { width: '20%', targets: 0 }
        ],
        fixedColumns: true
    } );
} );

Gotten from HERE

like image 35
Rotimi Avatar answered Sep 20 '22 13:09

Rotimi