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
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 )
andoTable.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:
- http://datatables.net/forums/discussion/7395/sscrolly-and-sscrollx-broken/p1
- http://datatables.net/forums/discussion/7778/vertical-scrolling-messes-up-column-header-alignment#Item_1
- http://datatables.net/forums/discussion/3835/width-columns-problem-in-chrome-safari/p1
- My bug report: http://datatables.net/forums/discussion/12491/datatables-header-alignment-issue-refresh#Item_1
This issue has driven me nuts! Please contribute your thoughts!
try this, the following code solved my problem
for more information pls refer Here.
I have fixed the column issue bu using below concept
For bootstrap users, this fixed it for me:
See article here
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.
change the
to
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.
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.
Faced the same issue recently and was still searching for the solution when I tried something on my css, check if adding to your cells (th and td)
will resolve this issue; for me, I was using some html/css framework that was adding a the value
border-box
in every element.