我已经在 datatables.net 论坛上发布了这个,但是一周后,仍然没有任何回应。希望我能在这里找到帮助...
我正在使用数据表版本 1.8.1,并且在启用垂直滚动的情况下对列标题对齐进行了噩梦。
使用下面发布的代码, header 在 Firefox 和 IE8 和 IE9 中正确排列,但 Chrome 和 IE7 关闭。我在该项目中使用了很多 数据表,这是每个数据表的问题。我迫切需要帮助!
编辑:我发现这与设置表格宽度有关。数据表采用其容器的宽度。如果我没有设置宽度,一切都很好(但表格对于我在页面上需要它的地方来说太大了)。如果我给表格的 div(或更高处的父 div)一个宽度,标题就不会正确排列。
谢谢!!
截图:
www.dennissheppard.net/firefox_alignment.png
www.dennissheppard.net/chrome_alignment.png
www.dennissheppard.net/ie7_alignment.png
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':[]
});
<table id="order_review_grid" class="grid_table" cellpadding="0px" cellspacing="0px">
<thead class="grid_column_header_row" id="order_review_grid_column_header_row">
<tr>
<td class="" id='sequenceNumber'>SEQ #</td>
<td class="grid_sc_header" id='statusCode'>Sc</td>
<td class="grid_sc_header" id='onOrderGuide'>O.G.</td>
<td class="grid_image_header" id='image'>Image</td>
<td class="grid_description_header" id='description'>Description</td>
<td class="grid_brand_header" id='label'>Brand</td>
<td class="grid_pack_header" id='packSize'>Pack</td>
<td class="grid_price_header" id='price'>Price</td>
<td class="grid_qtrfull_header" id='caseQuantity'>Full</td>
<td class="grid_qtrypart_header" id='eachQuantity'>Partial</td>
<td class="grid_refnum_header" id='referenceNumber'>Ref #</td>
<td class="grid_refnum_header"> </td>
</tr>
</thead>
<tbody class="">
<!-- loaded data will go here -->
</tbody>
</table>
最佳答案
我也遇到了同样的问题。它在 Mozilla Firefox、Opera 中运行良好(像素完美列对齐),但在 Chrome 21 中运行不正常。
解决方法:
就像这篇文章中提到的http://datatables.net/forums/discussion/3835/width-columns-problem-in-chrome-safari/p1
Basically what is happening, is that DataTables is trying to read the width of the table, that the browser has drawn, so it can make the header match. The problem is that when DataTables does this calculation on your page, the browser hasn't shown the scrollbar - and hence the calculation is slightly wrong! The reason I suggest that it's a Webkit bug, is that, even after DataTables has run through all of it's logic, the scrollbar still hasn't been displayed. If you add the following code you can see the effect of this:
console.log( $(oTable.fnSettings().nTable).outerWidth() ); setTimeout( function () { console.log( $(oTable.fnSettings().nTable).outerWidth() ); }, 1 );
有趣的是,在我添加 setTimeout 并执行后,仍有一列未对齐。添加 "sScrollX": "100%"后, "sScrollXInner": "100%"所有列都对齐(像素完美)。
Chrome/Chromium 的解决方案,FF、Opera、IE9 中的源代码:
$(document).ready(function()
{
var oTable = $('#mytable').dataTable(
{
"sScrollY": ( 0.6 * $(window).height() ),
"bPaginate": false,
"bJQueryUI": true,
"bScrollCollapse": true,
"bAutoWidth": true,
"sScrollX": "100%",
"sScrollXInner": "100%"
});
setTimeout(function ()
{
oTable.fnAdjustColumnSizing();
}, 10 );
});
关于javascript - jQuery 数据表标题与垂直滚动不对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8607290/