DataTables fixed headers misaligned with columns in wide tables

datatables scrolly header not aligned
jquery datatable header misaligned
datatables fixed column header
datatables fixed header vertical scroll
html table misaligned columns
datatable scrolly header problem
fixed header table
datatables fixed header inside div
Problem

When using the sScrollX, sScrollXInner and/or sScrollY to achieve a fixed header table with its inner content scrolling, the headers of the table go out of alignment with the rest of the body in Chrome and IE. Firefox, on the other hand, displays them perfectly.

Using the version 1.9.4, as far as I can tell, this issue only occurs when there is a lot of data with fluctuating widths, and with words that are very long/unwrappable combined in the same columns as small ones. Also, the table in question needs to be fairly wide.

All these factors are demonstrated in this fiddle:

Output

Chrome:

IE:

Firefox

Suggested Solutions

These solutions have been suggested before but have had no effect on my implementation. Owing to some of these suggestions, I setup a clean plain vanilla demo as I wanted to ensure that no other code was contributing to this effect.

  • turn-off/remove all my css
  • setTimeout( function () { oTable.fnAdjustColumnSizing(); }, 10 );
  • calling oTable.fnFilter( "x",0 ) and oTable.fnFilter( "",0 ) in that order
  • "sScrollXInner": "100%"
  • get rid of all widths

The only solution that I found to the misaligned headers was taking out sScrollX and sScrollY, but this can't be counted as a solution as you lose the fixed header/inner content scrolling functionality. So sadly it's a temporary hack, not a fix!

Note

To edit/play with the latest fiddle.

I have tried various combinations which can be observed in the revision history of the fiddle by using the link http://jsfiddle.net/pratik136/etL73/#REV# where 1 <= #REV# <= 12

History

StackO This question has been asked before: jQuery Datatables Header Misaligned With Vertical Scrolling but the vital difference is that the OP of that question mentioned that they were able to fix the issue if all CSS was removed, which is untrue in my case, and I have tried a few permutations, thus thought the question worthy of a repost.

External This issue has also been flagged on the DataTables forum:

This issue has driven me nuts! Please contribute your thoughts!

EDIT: See the latest Fiddle with "fixed header":


The Fiddle.

One of the solutions is to implement scrolling yourself instead of letting DataTables plugin do it for you.

I've taken your example and commented out sScrollX option. When this option is not present DataTables plugin will simply put your table as is into a container div. This table will stretch out of the screen, therefore, to fix that we can put it into a div with required width and an overflow property set - this is exactly what the last jQuery statement does - it wraps existing table into a 300px wide div. You probably will not need to set the width on the wrapping div at all (300px in this example), I have it here so that clipping effect is easily visible. And be nice, don't forget to replace that inline style with a class.

$(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": 't',
    "sDom": '<"H"CTrf>t<"F"lip>',
    "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
    //"sScrollY": "500px",
    //"sScrollX": "100%",
    "sScrollXInner": "110%",
    "fnInitComplete": function() {
        this.css("visibility", "visible");
    }
});

var tableId = 'PeopleIndexTable';
$('<div style="width: 300px; overflow: auto"></div>').append($('#' + tableId)).insertAfter($('#' + tableId + '_wrapper div').first())});

6. Warning: Possible column misalignment, will occur when DataTables is unable to align the columns correctly between the different tables. The effect can be a little disconcerting for end-users since header ,� DataTables fixed headers misaligned with columns in wide tables it wraps existing table into a 300px wide div. You probably will not need to set the width on the

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>

ScrollY: misaligned table headers with bootstrap — DataTables forums, With ScrollY enabled, our table headers are misaligned (too narrow). Thanks Allan, it's now fixed although I understand it's a hack on my part. but if I applied all changes I see very wide columns (full screen on 1920x1080). 1. fixed header / footer with scrolling using the main browser scroll bars 2. left and right columns should be fixed. Datatables provides 2 plugins fixedheader & foxedcolumn that provides these features. I am unable to make the two plugins to work together. I am facing following issues:

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>'
} );

jquery DataTables fixed headers misaligned with columns in wide , Problem When using the sScrollX, sScrollXInner and/or sScrollY to achieve a fixed header table with its inner content scrolling, the headers of the table go out of� I'm having a hard time finding a way to redraw the column headings when using Fixed Headers, when resizing the browser window. The headers remain in their original width until I force a refresh of the screen, or change the order of the columns. I have tried the fixes suggested in the following two posts but have not been successful.

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.

Tables in HTML documents, jquery DataTables fixed headers misaligned with columns in wide tables? $( document).ready(function() { var stdTable1 = $(".standard-grid1").dataTable({� I have a table with two rows of headers, but only the 2nd header is showing up when creating a csv. I have seen another similar post but it was not answered:

For bootstrap users, this fixed it for me:

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

See article here

[Table] Columns get misaligned in dense tables � Issue #6058, DataTables fixed headers misaligned with columns in wide tables. 由匿名(未验证) 提交于2019-12-03 02:23:02. 可以将文章内容翻译成中文,广告屏蔽插件可能会� Two tables with FixedHeaders on a single page Change the display order of the fixed elements (zIndex) A spreadsheet style layout with header and footer, left and right columns all fixed.

Template:Static column begin, Table cells may either contain "header" information (see the TH element) or "data " (see the TD element). #IMPLIED -- column width specification -- %cellhalign; -- horizontal alignment in cells If the table width is given a fixed value via the width attribute of the TABLE The first ten columns will be 15 pixels wide each. I noticed that the reason for the misaligned table width is due to the width being explicitly set to 100px initally, instead of the correctly calculated width. This may not address the issue at hand, but a workaround is to force the table and wrapper div elements to width:100%.

jQuery DataTables: Column width issues with Bootstrap tabs , The expanded part is now only one column wide. Of course, fixed headers and/ or columns are also an important feature, but I think these andrewseguin changed the title md-table: column misalignment in dense tables� The FixedHeader plug-in for DataTables will float the 'thead' element above the table at all times to help address this issue. The column titles also remain click-able to perform sorting. Key features include: Fix the header to the top of the window; Ability to fix the footer and left / right columns as well; z-Index ordering options

A template for easily creating a numbered static column to the left of a sorted table. Example of a very wide table made narrower with a multi-row header even when class=wikitable and class=mw-datatable are removed from the table. Header-valign: The vertical alignment of the text in the row-number column header.

Comments
  • For me your fiddle is fine in Chrome, but disaligned in Firefox.
  • Ruslans - this is a great start! Thanks! Could I ask if this solution can be tweaked to allow the sScrollY to be set? At the moment, if that is set, the header understandably displays below the wrapped table.
  • So what you really want is for table to be scrolled both horizontally and vertically. In that case keep sScrollY and sScrollX properties commented out and just constrain the wrapping div to your required vertical size, e.g. 100px in the following Fiddle: jsfiddle.net/WZrCx
  • Well, not quite! Using sScrollY, I can achieve a fixed header effect, whereas wrapping it doesn't.
  • In that case you can do something like this: jsfiddle.net/ruslans_uralovs/zDaGk
  • @RuslansUralovs: I find you last solution more efficient than one provided by DataTables. In case when table is set as some percentage from window width (90%) and window is resized so that both vertical and horizontal scrolling is needed, DataTables header starts to fall over. The only luxury which I cannot afford is duplication of data. Unfortunately some of the tables I have might be very big, so having DOM doubled is unaffordable. I'll try to implement the solution when "table with header" has no body, but on window resize the widths of the columns are copied from "table with body".
  • Thanks. This hasn't fixed the issue for me yet, but it got me somewhere!
  • Nice answer. I had a different problem but your code helped me fix it. The two first changes you said were the key.
  • Can you recreate this fix on the jsFiddle sandbox that I have provided?
  • Have you tried to apply it to the fiddle? Did it work?
  • This should be the right answer. I just add the adjustment after the init is complete $('.dataTable').dataTable().on( 'init', function () { $(this).columns.adjust() .fixedColumns().relayout(); })
  • Nope, doesn't fix. Even in IE9/10. Did you try it on the Fiddle provided?