dataTables JS Plugin responsive width overflows se

2019-05-07 01:59发布

Using the DataTables plugin for displaying tabular data I found that when setting a percentage width on the tables container div the plugin was performing a width calculation and setting a pixel width on the table which was actually wider than the div.

This wasn't an issue when using the table at full width in responsive mode, but when trying at halfwidth it overshot the set width and forced the browser window to scroll horizontally.

Code:

$('#' + tableID).dataTable({
    "dom": 'Rlfrtip',
    "ajax" : file,
    "responsive": true,
    "columns" : columnDataMap,
    "order": [[ 1, "desc" ]],
    "sAjaxDataProp" : "items",
    colReorder: {
        fixedColumns: 1
    },
    "columnDefs": [ {
        "targets": 0,
        "sortable": false,
        "data": null,
        "defaultContent": '...'
    } ]
});

1条回答
Luminary・发光体
2楼-- · 2019-05-07 02:41

You can stop DataTables from adding a (faulty) width to the table by adding the following option when initialising the plugin:

"bAutoWidth":false,

End product:

$('#' + tableID).dataTable({
    "dom": 'Rlfrtip',
    "ajax" : file,
    "responsive": true,
    "bAutoWidth":false,  
    "columns" : columnDataMap,
    "order": [[ 1, "desc" ]],
    "sAjaxDataProp" : "items",
    colReorder: {
        fixedColumns: 1
    },
    "columnDefs": [ {
        "targets": 0,
        "sortable": false,
        "data": null,
        "defaultContent": '...'
    } ]
});
查看更多
登录 后发表回答