Duplicate empty header occur in datatable when enabling scrollX or scrollY when using Google Chrome

有些话、适合烂在心里 提交于 2019-12-03 07:40:26

I had the same issue with firefox & firefox developer edition. The root cause is, when we set scrollX:true, datatable adds an additional div with a table and a header inside, apart from the table & header already constructed. This is to get the scrolling within table effect.

Datatable, tries to set the height to 0px, to hide it. Some browsers don't interpret this properly.

<tr style="height: 0px;" role="row"> 

To hide it, changing the style to this row to 'hidden' will break the structure of the table. The working solution would be, to have visibility:'collapse'

Sample datatable configuration:

        tableElement.DataTable({
            "scrollX": true,
            "initComplete": function(settings, json) {
                $('.dataTables_scrollBody thead tr').css({visibility:'collapse'});
            }
            //other datatable configurations...  
        });

since it's a table, we need to have visibility:'collapse' and not visibility:'hidden' - more info on visibility css property

Solution 1 :

To Resolve This Please Use "scrollXInner": true Avoid to Using "scrollX": true"

var tableI = $('#table_data').DataTable({
    "bLengthChange": false,
    "scrollX": true, //Do Not Use This (Remove This Line)
    "scrollXInner": true //Use This (Add This Line)
     "dom": 'frtp'
});

Solution 2 :

OR You Can Add This CSS

.dataTables_scrollBody thead tr[role="row"]{
    visibility: collapse !important;
}

I had the same issue on Google Chrome. This was the fix:

.dataTable(
        {
          "processing": false,
          "serverSide": false,
          "autoWidth": true,
          "columnDefs": [
            {"orderable": false, "targets": [5, 6]}
          ],
          "order": [1, 'asc'],
          "dom": 'l<"toolbar">frtip',
          drawCallback: function () { // this gets rid of duplicate headers
              $('.dataTables_scrollBody thead tr').css({ display: 'none' }); 
          },
          scrollY: '65vh',
          scrollCollapse: true,
          paging: false
        });

The solution was simple in my case. Just include this on your css:

/* Override sorting header --> DataTable Bug */
.dataTables_scrollBody > table > thead > tr {
    visibility: collapse;
    height: 0px !important;
}

Good explanation by Sairam. Better way would be to use just a CSS

div.dataTables_scrollBody thead {           
    display: none;
}

Try something like this:

"scrollX": '100%',

"scrollXInner": '125%',

This worked for me:

$('#DataTables_Table_0').on( 'draw.dt' function() {
    $('.dataTables_scrollBody thead tr').addClass('hidden')
}

Reference: geam's March 2015 answer on datatables.net forum

The best way is to use

dataTable.columns.adjust();

on init/draw callback

Swati

You can use Following code in your CSS:

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