dataTables JS Plugin responsive width overflows set div width

青春壹個敷衍的年華 提交于 2020-01-02 06:29:51

问题


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:


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": '...'
    } ]
});


来源:https://stackoverflow.com/questions/25345550/datatables-js-plugin-responsive-width-overflows-set-div-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!