DataTables fixed headers misaligned with columns in wide tables

前端 未结 21 1532
悲&欢浪女
悲&欢浪女 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:17

    I am having the same issue on IE9.

    I will just use a RegExp to strip all the white spaces before writing the HTML to the page.

    var Tables=$('##table_ID').html();
    var expr = new RegExp('>[ \t\r\n\v\f]*<', 'g');
    Tables= Tables.replace(expr, '><');
    $('##table_ID').html(Tables);
    oTable = $('##table_ID').dataTable( {
      "bPaginate": false,
      "bLengthChange": false,
      "bFilter": false,
      "bSort": true,
      "bInfo": true,
      "bAutoWidth": false,
      "sScrollY": ($(window).height() - 320),
      "sScrollX": "100%",
      "iDisplayLength":-1,
      "sDom": 'rt<"bottom"i flp>'
    } );
    
    0 讨论(0)
  • 2020-11-30 20:17

    I had the same problem and this code solves it. I got this solution from this article but I had to adjust the time on the interval for it to work.

    setTimeout(function(){
            oTable.fnAdjustColumnSizing();
    },50);
    
    0 讨论(0)
  • 2020-11-30 20:18

    I know there has been an answer flagged already, but for someone with similar problems that the above does not work for. I'm using it in conjunction with bootstrap theme.

    There is a line that can be altered in the dataTables.fixedHeader.js file. the default layout of the function looks as follows.

        _matchWidths: function (from, to) {
            var get = function (name) {
                return $(name, from)
                    .map(function () {
                        return $(this).width();
                    }).toArray();
            };
    
            var set = function (name, toWidths) {
                $(name, to).each(function (i) {
                    $(this).css({
                        width: toWidths[i],
                        minWidth: toWidths[i]
                    });
                });
            };
    
            var thWidths = get('th');
            var tdWidths = get('td');
    
            set('th', thWidths);
            set('td', tdWidths);
        },
    

    change the

        return $(this).width();
    

    to

        return $(this).outerWidth();
    

    outerWidth() is a function that is contained in jquery.dimensions.js if you are usingg a newer version of jquery. ie. jquery-3.1.1.js

    what the above function does is it maps the th of the original table, then applies them to the "cloned" table(fixed header). in my case they were always off by about 5px to 8px when misaligned. Most probably not the best fix out there but provides a solution to all other tables in the solution without having to specify it per table declaration. It has only been tested in Chrome so far, but it should provide an adjudicate solution on all browsers.

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

    I use this for Automatic column hiding as per column data, in that case, sometimes its break table structure. i solve that problem with this $($.fn.dataTable.tables(true)).DataTable().columns.adjust(); and this function $('#datatableId').on('draw.dt', function () { }); call after tabledata load.

    $('#datatableId').on('draw.dt', function () {
        $($.fn.dataTable.tables(true)).DataTable().columns.adjust();
    })
    
    0 讨论(0)
  • 2020-11-30 20:22

    The following is a way to achieve a fixed header table, I don't know if it will be enough for your purpose. Changes are:

    1. use "bScrollCollapse" instead of "sScrollXInner"
    2. don't use fieldset to wrap table
    3. add a "div.box" css class

    It seems fully working on my local machine, but it's not fully working using Fiddle. It seems that Fiddle adds a css file (normalize.css) that in some way broke the plugin css (quite sure I can make fully working also in Fiddle adding some css clear rules, but not have time to investigate further now)

    My working code snippet is below. Hope this can help.

    <!DOCTYPE html>
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
      <script type='text/javascript' src='http://code.jquery.com/jquery-1.8.2.js'></script>
      <script type='text/javascript' src="http://datatables.net/release-datatables/media/js/jquery.dataTables.min.js"></script>
    
     <style type='text/css'>
           div.box {
           height: 100px;
           padding: 10px;
           overflow: auto;
           border: 1px solid #8080FF;
           background-color: #E5E5FF;
       }
    
      .standard-grid1, .standard-grid1 td, .standard-grid1 th {
        border: solid black thin;
       }
    </style>
    
    <script type='text/javascript'> 
    $(window).load(function(){
    $(document).ready(function() {
        var stdTable1 = $(".standard-grid1").dataTable({
            "iDisplayLength": -1,
            "bPaginate": true,
            "iCookieDuration": 60,
            "bStateSave": false,
            "bAutoWidth": false,
            //true
            "bScrollAutoCss": true,
            "bProcessing": true,
            "bRetrieve": true,
            "bJQueryUI": true,
            "sDom": '<"H"CTrf>t<"F"lip>',
            "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
            "sScrollX": "100%",
            //"sScrollXInner": "110%",
            "bScrollCollapse": true,
            "fnInitComplete": function() {
                this.css("visibility", "visible");
            }
        });
    });
    });
    
    </script>
    
    
    </head>
    <body>
    <div>
        <table class="standard-grid1 full-width content-scrollable" id="PeopleIndexTable">
            <thead>
              <!-- put your table header HTML here -->
           </thead>
           <tbody>
              <!-- put your table body HTML here -->
            </tbody>
        </table>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 20:23

    try this

    this works for me... i added the css for my solution and it works... although i didnt change anything in datatable css except { border-collapse: separate;}

    .dataTables_scrollHeadInner {    /*for positioning header when scrolling is applied*/
    padding:0% ! important
    }
    
    0 讨论(0)
提交回复
热议问题