DataTables fixed headers misaligned with columns in wide tables

前端 未结 21 1528
悲&欢浪女
悲&欢浪女 2020-11-30 19:23

Problem

When using the sScrollX, sScrollXInner and/or sScrollY to achieve a fixed header table with its inner content scroll

相关标签:
21条回答
  • 2020-11-30 20:01

    I just figured out a way to fix the alignment issue. Changing the width of div containing the static header will fix its alignment. The optimal width I found is 98%. Please refer to the code.

    The auto generated div:

    <div class="dataTables_scrollHead" style="overflow: hidden; position: relative; border: 0px none; width: 100%;">

    Width here is 100%, change it to 98% on initializing and reloading the data table.

    jQuery('#dashboard').dataTable({
        "sEcho": "1",
    
        "aaSorting": [[aasortvalue, 'desc']],
        "bServerSide": true,
        "sAjaxSource": "NF.do?method=loadData&Table=dashboardReport",
        "bProcessing": true,
        "sPaginationType": "full_numbers",
        "sDom": "lrtip", // Add 'f' to add back in filtering
        "bJQueryUI": false,
        "sScrollX": "100%",
        "sScrollY": "450px",
        "iDisplayLength": '<%=recordCount%>',
        "bScrollCollapse": true,
        "bScrollAutoCss": true,
        "fnInitComplete": function () {
            jQuery('.dataTables_scrollHead').css('width', '98%'); //changing the width
        },
        "fnDrawCallback": function () {
            jQuery('.dataTables_scrollHead').css('width', '98%');//changing the width
        }
    });
    
    0 讨论(0)
  • 2020-11-30 20:02

    For bootstrap users, this fixed it for me:

       $($.fn.dataTable.tables(true)).DataTable()
          .columns.adjust()
          .fixedColumns().relayout();
    

    See article here

    0 讨论(0)
  • 2020-11-30 20:06

    Instead using sScrollX,sScrollY use separate div style

    .scrollStyle
     {
      height:200px;overflow-x:auto;overflow-y:scroll;
     }
    

    Add below after datatable call in script

     jQuery('.dataTable').wrap('<div class="scrollStyle" />');
    

    Its working perfectly after many tries.

    0 讨论(0)
  • 2020-11-30 20:06

    I had this very same problem and seen a lot of complicated answers that never seemed to work. I did solve it by simply overriding the CSS (site.css) on the following and it fixed it in Chrome, IE, and Firefox:

    table.display td {
    padding: 0px 5px;
    }
    /* this is needed for IE and Firefox if using horizontal scroll*/
    table{
        max-width: none;
        min-height: 0%;
    }
    

    This overrides the CSS in datatable.jui.css

    0 讨论(0)
  • 2020-11-30 20:09

    The code below worked. Corrected the issue on I.E 9.0 atleast. Hope this helps

    var oTable =  $('#tblList').dataTable({
        "sScrollY": "320px",
        "bScrollCollapse": true,
        });
    
        setTimeout(function(){
            oTable.fnAdjustColumnSizing();
        },10);
    
    0 讨论(0)
  • 2020-11-30 20:10

    Adding these two lines fixed this problem for me

    "responsive": true,
    "bAutoWidth": true
    

    There is now a responsive plugin available: https://datatables.net/extensions/responsive/. However, in my experience I have found that there are still a few bugs. It's still the best solution I've found so far.

    0 讨论(0)
提交回复
热议问题