From d5eb393df9dd7d4610e04e9c3276c5b4951da89c Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Wed, 14 Aug 2013 12:15:18 +0100 Subject: [PATCH] New: DataTables tables resize automatically with window (and width="100%" attr) - DataTables tables previously did not automatically adjust their sizing when the window width was changed, which let to a lot of additional calls to fnAdjustColumnSizing in peoples code (and support questions in the forums). This commit add adjustment to the sizing automatically for tables width have width="100%" as an attribute (we can't use CSS since we can't know if it is relative or absolute sizing) - extending what went before. This has full compatiblity with scrolling and non-scrolling tables. A throttle is used to not bring IE to its knees... --- .datatables-commit-sync | 2 +- media/js/jquery.dataTables.js | 129 +++++++++++++++++++++++----------- 2 files changed, 89 insertions(+), 42 deletions(-) diff --git a/.datatables-commit-sync b/.datatables-commit-sync index 9d0b2736..f1015efb 100644 --- a/.datatables-commit-sync +++ b/.datatables-commit-sync @@ -1 +1 @@ -6ce86aedafd9cdc882b15c8fa069a156791ede89 +8401eab9328f17b363d5b693bd65caecd7a0c994 diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 9eecbea4..6f82ade4 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -356,22 +356,30 @@ /** * Adjust the table column widths for new data. Note: you would probably want to * do a redraw after calling this function! - * @param {object} oSettings dataTables settings object + * @param {object} settings dataTables settings object * @memberof DataTable#oApi */ - function _fnAdjustColumnSizing ( oSettings ) + function _fnAdjustColumnSizing ( settings ) { /* Not interested in doing column width calculation if auto-width is disabled */ - if ( oSettings.oFeatures.bAutoWidth !== false ) + if ( settings.oFeatures.bAutoWidth !== false ) { - _fnCalculateColumnWidths( oSettings ); - for ( var i=0 , iLen=oSettings.aoColumns.length ; i 0 ) { - oSettings.aoColumns[i].sWidth = _fnStringToCss( iWidth ); + column.sWidth = _fnStringToCss( iWidth ); } iCorrector++; } } var cssWidth = $(nCalcTmp).css('width'); - oSettings.nTable.style.width = (cssWidth.indexOf('%') !== -1) ? + table.style.width = (cssWidth.indexOf('%') !== -1) ? cssWidth : _fnStringToCss( $(nCalcTmp).outerWidth() ); nCalcTmp.parentNode.removeChild( nCalcTmp ); } if ( widthAttr ) { - oSettings.nTable.style.width = _fnStringToCss( widthAttr ); + table.style.width = _fnStringToCss( widthAttr ); - if ( ! oSettings._attachedResizing && - (oSettings.oScroll.sY !== '' || oSettings.oScroll.sX !== '') ) + if ( ! oSettings._reszEvt ) { - $(window).bind('resize.DT-'+oSettings.sInstance, function () { - _fnScrollDraw( oSettings ); - } ); - oSettings._attachedResizing = true; + $(window).bind('resize.DT-'+oSettings.sInstance, throttle( function () { + _fnAdjustColumnSizing( oSettings ); + } ) ); + + oSettings._reszEvt = true; } } } + // @todo Move into a private functions file or make a proper DT function of it + function throttle( fn ) { + var + frequency = 200, + last, + timer; + + return function () { + var + now = +new Date(), + args = arguments; + + if ( last && now < last + frequency ) { + clearTimeout( timer ); + + timer = setTimeout( function () { + last = now; + fn(); + }, frequency ); + } + else { + last = now; + fn(); + } + }; + } + + /** * Adjust a table's width to take account of scrolling * @param {object} oSettings dataTables settings object