mirror of
https://github.com/DataTables/DataTables.git
synced 2024-12-01 13:24:10 +01:00
Fix - thread #14114: State saved table breaks if columns reduced
- If the number of columns in the table were reduced, then the column filter state that was saved would be incorrect since it would define more columns that there were. Equally sorting could potentially be done on a column that no longer exists. - There were a few work around in the code already to try and address this a bit, bit not satisfactorily as seen by thread 14114. The real issue was that the columns were being detected after the state was being loaded - ideally we want to load state after the columns had been detected and throw away the state saved if the columns did not match since the table might be entirly different. - This is done by a little bit of reordering in the constructor, and actually simplifies the code a bit.
This commit is contained in:
parent
ab7e797965
commit
86fd198fdd
@ -199,14 +199,6 @@ if ( oSettings.iInitDisplayStart === undefined )
|
||||
oSettings._iDisplayStart = oInit.iDisplayStart;
|
||||
}
|
||||
|
||||
/* Must be done after everything which can be overridden by the state saving! */
|
||||
if ( oInit.bStateSave )
|
||||
{
|
||||
oSettings.oFeatures.bStateSave = true;
|
||||
_fnLoadState( oSettings, oInit );
|
||||
_fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState, 'state_save' );
|
||||
}
|
||||
|
||||
if ( oInit.iDeferLoading !== null )
|
||||
{
|
||||
oSettings.bDeferLoading = true;
|
||||
@ -292,16 +284,6 @@ else
|
||||
/* Add the columns */
|
||||
for ( i=0, iLen=aoColumnsInit.length ; i<iLen ; i++ )
|
||||
{
|
||||
/* Short cut - use the loop to check if we have column visibility state to restore */
|
||||
if ( oInit.saved_aoColumns !== undefined && oInit.saved_aoColumns.length == iLen )
|
||||
{
|
||||
if ( aoColumnsInit[i] === null )
|
||||
{
|
||||
aoColumnsInit[i] = {};
|
||||
}
|
||||
aoColumnsInit[i].bVisible = oInit.saved_aoColumns[i].bVisible;
|
||||
}
|
||||
|
||||
_fnAddColumn( oSettings, anThs ? anThs[i] : null );
|
||||
}
|
||||
|
||||
@ -311,6 +293,15 @@ _fnApplyColumnDefs( oSettings, oInit.aoColumnDefs, aoColumnsInit, function (iCol
|
||||
} );
|
||||
|
||||
|
||||
/* Must be done after everything which can be overridden by the state saving! */
|
||||
if ( oInit.bStateSave )
|
||||
{
|
||||
oSettings.oFeatures.bStateSave = true;
|
||||
_fnLoadState( oSettings, oInit );
|
||||
_fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState, 'state_save' );
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Sorting
|
||||
* Check the aaSorting array
|
||||
@ -330,7 +321,7 @@ for ( i=0, iLen=oSettings.aaSorting.length ; i<iLen ; i++ )
|
||||
}
|
||||
|
||||
/* If aaSorting is not defined, then we use the first indicator in asSorting */
|
||||
if ( oInit.aaSorting === undefined && oSettings.saved_aaSorting === undefined )
|
||||
if ( oInit.aaSorting === undefined )
|
||||
{
|
||||
oSettings.aaSorting[i][1] = oColumn.asSorting[0];
|
||||
}
|
||||
|
@ -68,6 +68,11 @@ function _fnLoadState ( oSettings, oInit )
|
||||
return;
|
||||
}
|
||||
|
||||
// Number of columns have changed - all bets are off, no restore of settings
|
||||
if ( oSettings.aoColumns.length !== oData.aoSearchCols.length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Store the saved state so it might be accessed at any time */
|
||||
oSettings.oLoadedState = $.extend( true, {}, oData );
|
||||
|
||||
@ -76,21 +81,14 @@ function _fnLoadState ( oSettings, oInit )
|
||||
oSettings.iInitDisplayStart = oData.iStart;
|
||||
oSettings._iDisplayLength = oData.iLength;
|
||||
oSettings.aaSorting = oData.aaSorting.slice();
|
||||
oSettings.saved_aaSorting = oData.aaSorting.slice();
|
||||
|
||||
/* Search filtering */
|
||||
$.extend( oSettings.oPreviousSearch, oData.oSearch );
|
||||
$.extend( true, oSettings.aoPreSearchCols, oData.aoSearchCols );
|
||||
|
||||
/* Column visibility state
|
||||
* Pass back visibility settings to the init handler, but to do not here override
|
||||
* the init object that the user might have passed in
|
||||
*/
|
||||
oInit.saved_aoColumns = [];
|
||||
for ( var i=0 ; i<oData.abVisCols.length ; i++ )
|
||||
{
|
||||
oInit.saved_aoColumns[i] = {};
|
||||
oInit.saved_aoColumns[i].bVisible = oData.abVisCols[i];
|
||||
/* Column visibility state */
|
||||
for ( var i=0 ; i<oData.abVisCols.length ; i++ ) {
|
||||
oSettings.aoColumns[i].bVisible = oData.abVisCols[i];
|
||||
}
|
||||
|
||||
_fnCallbackFire( oSettings, 'aoStateLoaded', 'stateLoaded', [oSettings, oData] );
|
||||
|
Loading…
Reference in New Issue
Block a user