Vertical Scrolling Messes up Column Header Alignment
Vertical Scrolling Messes up Column Header Alignment
Hi all!
I'm using version 1.8.1 and am having nightmares over column header alignment with vertical scrolling enabled. After using various hacks to get this to work (looping through the scrollBody headers that are hidden to grab the appropriate widths and then applying them to the scrollHeader headers.... hiding the scrollHeader row completely and writing my own div headers based on the widths from the hidden scrollBody headers... etc.), I'm at my wit's end.
If I can get it to work in one browser, it breaks in the others. Right now with the code posted below, the headers line up correctly in Firefox and IE8 and IE9, but Chrome and IE7 are off. I'm using a *lot* of datatables on this project, and this is a problem with everyone. I'm desperate for help!
I'm not using any of my worthless hacks with the code below, just setting sScrollY.
Thanks!!
Screenshots:
www.dennissheppard.net/firefox_alignment.png
www.dennissheppard.net/chrome_alignment.png
www.dennissheppard.net/ie7_alignment.png
datatable code:
[code]
otable = $('#order_review_grid').dataTable({
'fnRowCallback': function (nRow, strings, displayIndex, dataIndex) {
return formatRow(nRow, dataIndex);
},
'fnDrawCallback':function()
{
checkIfOrderSubmitted(this);
},
'aoColumnDefs':
[
{ 'bVisible': false, 'aTargets': [COL_PRODUCT] },
{ 'bSortable': false, 'aTargets': [COL_IMAGE, COL_DELETE] },
{ 'sClass': 'right_align', 'aTargets': [COL_PRICE] },
{ 'sClass': 'center_align', 'aTargets': [COL_BRAND,COL_PACK] },
{ 'sClass': 'left_align', 'aTargets': [COL_DESCRIPTION] }
],
'sDom': 't',
'sScrollY':'405px',
'bScrollCollapse':true,
'aaSorting':[]
});
[/code]
Html:
[code]
SEQ #
Sc
O.G.
Image
Description
Brand
Pack
Price
Full
Partial
Ref #
<!-- loaded data will go here -->
[/code]
I'm using version 1.8.1 and am having nightmares over column header alignment with vertical scrolling enabled. After using various hacks to get this to work (looping through the scrollBody headers that are hidden to grab the appropriate widths and then applying them to the scrollHeader headers.... hiding the scrollHeader row completely and writing my own div headers based on the widths from the hidden scrollBody headers... etc.), I'm at my wit's end.
If I can get it to work in one browser, it breaks in the others. Right now with the code posted below, the headers line up correctly in Firefox and IE8 and IE9, but Chrome and IE7 are off. I'm using a *lot* of datatables on this project, and this is a problem with everyone. I'm desperate for help!
I'm not using any of my worthless hacks with the code below, just setting sScrollY.
Thanks!!
Screenshots:
www.dennissheppard.net/firefox_alignment.png
www.dennissheppard.net/chrome_alignment.png
www.dennissheppard.net/ie7_alignment.png
datatable code:
[code]
otable = $('#order_review_grid').dataTable({
'fnRowCallback': function (nRow, strings, displayIndex, dataIndex) {
return formatRow(nRow, dataIndex);
},
'fnDrawCallback':function()
{
checkIfOrderSubmitted(this);
},
'aoColumnDefs':
[
{ 'bVisible': false, 'aTargets': [COL_PRODUCT] },
{ 'bSortable': false, 'aTargets': [COL_IMAGE, COL_DELETE] },
{ 'sClass': 'right_align', 'aTargets': [COL_PRICE] },
{ 'sClass': 'center_align', 'aTargets': [COL_BRAND,COL_PACK] },
{ 'sClass': 'left_align', 'aTargets': [COL_DESCRIPTION] }
],
'sDom': 't',
'sScrollY':'405px',
'bScrollCollapse':true,
'aaSorting':[]
});
[/code]
Html:
[code]
SEQ #
Sc
O.G.
Image
Description
Brand
Pack
Price
Full
Partial
Ref #
<!-- loaded data will go here -->
[/code]
This discussion has been closed.
Replies
If I get rid of all widths, the table lines up perfectly in all browsers.
But as soon as I try to set a width on either the table or a containing div, datatables either ignores the width and makes it much too wide, or if I use "important", the headers get misaligned.
So... still stuck.
https://picasaweb.google.com/109034534334401197927/DataTablesColumnIssue?authkey=Gv1sRgCKv2kp2nrsWpFg
Any suggestion?
Allan
The only trick that seemed to work is to set table-layout: fixed on o.nTable, o.nTHead.parentNode and o.nTFoot.parentNode when the detected browser is ie8 or 9. For the width calculation to run properly I remove the style attribute near the top of _fnScrollDraw, and put it back near the bottom.
Also, as the fixed tables column width apply without regards to the cell padding, I used innerWidth() instead of width() in the _fnApplyToChildren calls applying to the header and footer.
I applied the change in src/core/core.scrolling.js and recompiled. Tested on IE9 on an ajax-generated grid infinite-scrolling grid ( bScrollInfinite:true,bScrollCollapse:true,sScrollY:set,sAjaxSource:script.php ). Not tested yet in other contexts.
[code]
--- org/core.scrolling.js 2012-04-23 16:44:37.777678700 -0400
+++ mod/core.scrolling.js 2012-04-23 16:44:17.268628000 -0400
@@ -195,5 +195,6 @@
nScrollFootInner = (o.nTFoot !== null) ? o.nScrollFoot.getElementsByTagName('div')[0] : null,
nScrollFootTable = (o.nTFoot !== null) ? nScrollFootInner.getElementsByTagName('table')[0] : null,
- ie67 = $.browser.msie && $.browser.version <= 7;
+ ie67 = $.browser.msie && $.browser.version <= 7,
+ ie8up = $.browser.msie && $.browser.version >= 8;
/*
@@ -203,4 +204,10 @@
/* Remove the old minimised thead and tfoot elements in the inner table */
$(o.nTable).children('thead, tfoot').remove();
+ if (ie8up)
+ {
+ $(o.nTable).css("table-layout", "");
+ $(o.nTHead.parentNode).css("table-layout", "");
+ if (o.nTFoot) { $(o.nTFoot.parentNode).css("table-layout", ""); }
+ }
/* Clone the current header and footer elements and then place it into the inner table */
@@ -312,5 +319,5 @@
oStyle.height = 0;
- iWidth = $(nSizer).width();
+ iWidth = ie8up ? $(nSizer).innerWidth() : $(nSizer).width();
nToSize.style.width = _fnStringToCss( iWidth );
aApplied.push( iWidth );
@@ -332,5 +339,5 @@
oStyle.height = 0;
- iWidth = $(nSizer).width();
+ iWidth = ie8up ? $(nSizer).innerWidth() : $(nSizer).width();
nToSize.style.width = _fnStringToCss( iWidth );
aApplied.push( iWidth );
@@ -408,5 +415,10 @@
}
}
-
+ if (ie8up)
+ {
+ $(o.nTable).css("table-layout", "fixed");
+ $(o.nTHead.parentNode).css("table-layout", "fixed");
+ if (o.nTFoot) { $(o.nTFoot.parentNode).css("table-layout", "fixed"); }
+ }
/*
[/code]
Allan
Allan
Cheers,
Moolie
Thanks,
Allan
As you can hopefully see, the columns don't line up. My test environment is in IE9, and there it seems like the problem is that the scrollbars are not accounted for when the widths are calculated. The best way to see that here is to look at the render using the real-time side-by-side preview, to be sure that the vertical scrollbar shows up. When the vertical scrollbar is present and the horizontal scrollbar is scrolled all the way to the right, you can see fairly clearly the issue that I believe we're all talking about.
As a side note, there is another issue I found that can also be seen with this example, when viewed in chrome at least (it also affects my test environment in IE9, but somehow does not apply when viewed in this debugger).
When bScrollCollapse = true and the width of the page is < the width of the table, a horizontal scrollbar appears at the bottom of the datatable, which is a perfectly normal behavior, and works just fine. However, when you then use the search filter box and then clear it out, the table expands to its normal max height, and the scrollbar at the bottom is no longer there. This behavior can be seen in the example here. Although it doesn't seem to cause any problems in the debugger here, it does cause mis-alignments between the locked columns and the table body in my test environment
In order to fix this, i added one line (nSizer.style.minWidth =nSizer.style.width;) inside _fnScrolldraw method. I haven't verified this for side-effects but its working for me in almost all browsers.
_fnApplyToChildren( function(nSizer) {
nSizer.innerHTML = "";
nSizer.style.width = _fnStringToCss( aApplied.shift() );
nSizer.style.minWidth =nSizer.style.width;
}, anHeadSizers );
I request others to try this fix and if it works, we can ask Allan to include this in next release
Thanks for this wonderful tool. It works like a charm. Except for this alignment issue :(
Has any workaround been done for this? I'm using V1.9.1 of Datatables, and getting messy alignment of the header and column with vertical scrollbar, and also if I click the sort of any of the columns, the horizontal scrollbar goes away and occupies the entire width of the screen.
I have used
[code]
setTimeout(function () {
oTable.fnAdjustColumnSizing();
}, 10);
[/code]
this piece of code to get the horizontal scrollbar properly during the initial loading, but whenever i perform a sort, the horizontal scrollbar gets collapsed :( Can I call this function again, somewhere to fix the horizontal scrollbar issue?
Hi Tjain1981,
Can you pls tell in which file '_fnScrolldraw' method modification needs to be added to make it work?
thanks in advance,
Jai
Facing same issue, I read with interest this thread and some other post over the net.
After many hours working on this, I found a solution adding the code below to the end of the fnDrawCallback :
[code]
//remove the inline-style 'width' from #myTable
$('#myTable').attr('style', $('#myTable').attr('style').replace(/width[^;]+;?/g, ''));
//retrieve the real width of #myTable and set it to the table header and its container
$('#myTable').closest('.dataTables_scroll').find('.dataTables_scrollHeaderInner, .dataTables_scrollHeaderInner > table.dataTable').css('width', $('#myTable').innerWidth()+'px');
[/code]
Hoping that can help you :-)
Jérôme
Allan
I have grabbed 2 screenshots - where can I post them?
I will now try to give some details.
I have a datatable that starts empty and I initialise it as a datatable and then add data to it. The table is in a div and I initialise with scroll y set. When the table first builds the header width fills the whole div width and then the scrollbar pushes the tbody across so that the header and and body do not line up.
If I filter the table so that the body does not require a scrollbar then it lines up. Interestingly when I remove the filter, the scrollbar appears and the header does line up with the tbody!
I suspect that the problem lies with the tbody getting squeezed when the scrollbar appears and the header (being in a seperate div) does not know about it. If datatables is initialised in a div that is already loaded with data to the point of scrolling then this may not happen.
I hope this helps - this bug is a major problem for me.
for an example please check this URL: http://live.datatables.net/edeset
is there any method to override the style of this DIV in my case problematically as i deal with bi directional web sites, that switches from Right-to-left and Left-to-right
@watherton - Please link to a test case showing the problem you are facing. Without a test case I don't know what is going wrong.
As noted in a number of other discussions in the forums, DataTables does not support rows in column orientation at this time.
Allan