Skip to content

Commit

Permalink
Fix: Add redrawCalculations option to dt-api columns().visible()
Browse files Browse the repository at this point in the history
…and `dt-api column().visible()` to improve performance in a loop.
  • Loading branch information
Allan Jardine committed Jul 25, 2014
1 parent 335b1bd commit e227ba9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
14 changes: 13 additions & 1 deletion docs/api/column().visible().xml
Expand Up @@ -11,11 +11,14 @@
</type>

<type type="function">
<signature>column().visible( show )</signature>
<signature>column().visible( show [, redrawCalculations ] )</signature>
<description>Set the visibility of the selected column.</description>
<parameter type="boolean" name="show">
Specify if the column should be visible (`true`) or not (`false`).
</parameter>
<parameter type="boolean" name="redrawCalculations" default="true">
Indicate if DataTables should recalculate the column layout (`true` - default) or not (`false`). Typically this would be left as the default value, but it can be useful to disable when using the method in a loop - so the calculations are performed on every call as they can hamper performance.
</parameter>
<returns type="DataTables.Api">DataTables API instance with selected column in the result set.</returns>
</type>

Expand All @@ -35,6 +38,15 @@ alert( 'Column index 0 is '+
var table = $('#example').DataTable();
table.column( 0 ).visible( false );
]]></example>

<example title="Hide multiple columns using `redrawCalculations` to improve performance"><![CDATA[
var table = $('#example').DataTable();
for ( var i=0 ; i<4 ; i++ ) {
table.column( i ).visible( false, false );
}
table.columns.adjust().draw( false ); // adjust column sizing and redraw
]]></example>

<related type="api">columns().visible()</related>
Expand Down
12 changes: 11 additions & 1 deletion docs/api/columns().visible().xml
Expand Up @@ -11,11 +11,14 @@
</type>

<type type="function">
<signature>columns().visible( show )</signature>
<signature>column().visible( show [, redrawCalculations ] )</signature>
<description>Set the visibility of the selected columns.</description>
<parameter type="boolean" name="show">
Specify if the columns should be visible (`true`) or not (`false`).
</parameter>
<parameter type="boolean" name="redrawCalculations" default="true">
Indicate if DataTables should recalculate the column layout (`true` - default) or not (`false`). Typically this would be left as the default value, but it can be useful to disable when using the method in a loop - so the calculations are performed on every call as they can hamper performance.
</parameter>
<returns type="DataTables.Api">DataTables API instance with selected columns in the result set.</returns>
</type>

Expand All @@ -36,6 +39,13 @@ alert( 'Table's column visibility are set to: '+table.columns().visible().join('
var table = $('#example').DataTable();
table.columns( '.detail' ).visible( false );
]]></example>

<example title="Hide multiple columns using `redrawCalculations` to improve performance"><![CDATA[
var table = $('#example').DataTable();
table.columns( [ 0, 1, 2, 3 ] ).visible( false, false );
table.columns.adjust().draw( false ); // adjust column sizing and redraw
]]></example>

<related type="api">column().visible()</related>
Expand Down
18 changes: 10 additions & 8 deletions js/api/api.columns.js
Expand Up @@ -82,7 +82,7 @@ var __column_selector = function ( settings, selector, opts )



var __setColumnVis = function ( settings, column, vis ) {
var __setColumnVis = function ( settings, column, vis, recalc ) {
var
cols = settings.aoColumns,
col = cols[ column ],
Expand Down Expand Up @@ -125,12 +125,14 @@ var __setColumnVis = function ( settings, column, vis ) {
_fnDrawHead( settings, settings.aoHeader );
_fnDrawHead( settings, settings.aoFooter );

// Automatically adjust column sizing
_fnAdjustColumnSizing( settings );
if ( recalc === undefined || recalc ) {
// Automatically adjust column sizing
_fnAdjustColumnSizing( settings );

// Realign columns for scrolling
if ( settings.oScroll.sX || settings.oScroll.sY ) {
_fnScrollDraw( settings );
// Realign columns for scrolling
if ( settings.oScroll.sX || settings.oScroll.sY ) {
_fnScrollDraw( settings );
}
}

_fnCallbackFire( settings, null, 'column-visibility', [settings, column, vis] );
Expand Down Expand Up @@ -217,11 +219,11 @@ _api_registerPlural( 'columns().nodes()', 'column().nodes()', function () {



_api_registerPlural( 'columns().visible()', 'column().visible()', function ( vis ) {
_api_registerPlural( 'columns().visible()', 'column().visible()', function ( vis, calc ) {
return this.iterator( 'column', function ( settings, column ) {
return vis === undefined ?
settings.aoColumns[ column ].bVisible :
__setColumnVis( settings, column, vis );
__setColumnVis( settings, column, vis, calc );
} );
} );

Expand Down

0 comments on commit e227ba9

Please sign in to comment.