Skip to content

Commit

Permalink
Fix: Reordering initial data load to allow data-* attributes to be …
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanJard committed May 17, 2024
1 parent df5e548 commit d802465
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 133 deletions.
154 changes: 65 additions & 89 deletions js/core/core.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,110 +295,86 @@ if ( rowOne.length ) {
} );
}

// Must be done after everything which can be overridden by the state saving!
_fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState );

var features = oSettings.oFeatures;
var loadedInit = function () {
/*
* Sorting
* @todo For modularisation (1.11) this needs to do into a sort start up handler
*/
if ( oInit.bStateSave )
{
features.bStateSave = true;
}

// If aaSorting is not defined, then we use the first indicator in asSorting
// in case that has been altered, so the default sort reflects that option
if ( oInit.aaSorting === undefined ) {
var sorting = oSettings.aaSorting;
for ( i=0, iLen=sorting.length ; i<iLen ; i++ ) {
sorting[i][1] = oSettings.aoColumns[ i ].asSorting[0];
}
// If aaSorting is not defined, then we use the first indicator in asSorting
// in case that has been altered, so the default sort reflects that option
if ( oInit.aaSorting === undefined ) {
var sorting = oSettings.aaSorting;
for ( i=0, iLen=sorting.length ; i<iLen ; i++ ) {
sorting[i][1] = oSettings.aoColumns[ i ].asSorting[0];
}
}

/* Do a first pass on the sorting classes (allows any size changes to be taken into
* account, and also will apply sorting disabled classes if disabled
*/
_fnSortingClasses( oSettings );

_fnCallbackReg( oSettings, 'aoDrawCallback', function () {
if ( oSettings.bSorted || _fnDataSource( oSettings ) === 'ssp' || features.bDeferRender ) {
_fnSortingClasses( oSettings );
}
} );


/*
* Final init
* Cache the header, body and footer as required, creating them if needed
*/
var caption = $this.children('caption');

if ( oSettings.caption ) {
if ( caption.length === 0 ) {
caption = $('<caption/>').appendTo( $this );
}
/* Do a first pass on the sorting classes (allows any size changes to be taken into
* account, and also will apply sorting disabled classes if disabled
*/
_fnSortingClasses( oSettings );

caption.html( oSettings.caption );
_fnCallbackReg( oSettings, 'aoDrawCallback', function () {
if ( oSettings.bSorted || _fnDataSource( oSettings ) === 'ssp' || features.bDeferRender ) {
_fnSortingClasses( oSettings );
}
} );

// Store the caption side, so we can remove the element from the document
// when creating the element
if (caption.length) {
caption[0]._captionSide = caption.css('caption-side');
oSettings.captionNode = caption[0];
}

if ( thead.length === 0 ) {
thead = $('<thead/>').appendTo($this);
}
oSettings.nTHead = thead[0];
$('tr', thead).addClass(oClasses.thead.row);
/*
* Table HTML init
* Cache the header, body and footer as required, creating them if needed
*/
var caption = $this.children('caption');

var tbody = $this.children('tbody');
if ( tbody.length === 0 ) {
tbody = $('<tbody/>').insertAfter(thead);
if ( oSettings.caption ) {
if ( caption.length === 0 ) {
caption = $('<caption/>').appendTo( $this );
}
oSettings.nTBody = tbody[0];

var tfoot = $this.children('tfoot');
if ( tfoot.length === 0 ) {
// If we are a scrolling table, and no footer has been given, then we need to create
// a tfoot element for the caption element to be appended to
tfoot = $('<tfoot/>').appendTo($this);
}
oSettings.nTFoot = tfoot[0];
$('tr', tfoot).addClass(oClasses.tfoot.row);
caption.html( oSettings.caption );
}

// Check if there is data passing into the constructor
if ( oInit.aaData ) {
for ( i=0 ; i<oInit.aaData.length ; i++ ) {
_fnAddData( oSettings, oInit.aaData[ i ] );
}
}
else if ( _fnDataSource( oSettings ) == 'dom' ) {
// Grab the data from the page
_fnAddTr( oSettings, $(oSettings.nTBody).children('tr') );
}
// Store the caption side, so we can remove the element from the document
// when creating the element
if (caption.length) {
caption[0]._captionSide = caption.css('caption-side');
oSettings.captionNode = caption[0];
}

/* Copy the data index array */
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
if ( thead.length === 0 ) {
thead = $('<thead/>').appendTo($this);
}
oSettings.nTHead = thead[0];
$('tr', thead).addClass(oClasses.thead.row);

/* Initialisation complete - table can be drawn */
oSettings.bInitialised = true;
var tbody = $this.children('tbody');
if ( tbody.length === 0 ) {
tbody = $('<tbody/>').insertAfter(thead);
}
oSettings.nTBody = tbody[0];

/* Check if we need to initialise the table (it might not have been handed off to the
* language processor)
*/
if ( bInitHandedOff === false ) {
_fnInitialise( oSettings );
}
};
var tfoot = $this.children('tfoot');
if ( tfoot.length === 0 ) {
// If we are a scrolling table, and no footer has been given, then we need to create
// a tfoot element for the caption element to be appended to
tfoot = $('<tfoot/>').appendTo($this);
}
oSettings.nTFoot = tfoot[0];
$('tr', tfoot).addClass(oClasses.tfoot.row);

/* Must be done after everything which can be overridden by the state saving! */
_fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState );
// Copy the data index array
oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();

if ( oInit.bStateSave )
{
features.bStateSave = true;
_fnLoadState( oSettings, oInit, loadedInit );
}
else {
loadedInit();
}
// Initialisation complete - table can be drawn
oSettings.bInitialised = true;

// Check if we need to initialise the table (it might not have been handed off to the
// language processor)
if ( bInitHandedOff === false ) {
_fnInitialise( oSettings );
}
103 changes: 59 additions & 44 deletions js/core/core.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
function _fnInitialise ( settings )
{
var i, iAjaxStart=settings.iInitDisplayStart;
var init = settings.oInit;
var dataSrc = _fnDataSource( settings );

/* Ensure that the table data is fully initialised */
// Ensure that the table data is fully initialised
if ( ! settings.bInitialised ) {
setTimeout( function(){ _fnInitialise( settings ); }, 200 );
return;
Expand All @@ -21,52 +23,65 @@ function _fnInitialise ( settings )
_fnDrawHead( settings, settings.aoHeader );
_fnDrawHead( settings, settings.aoFooter );

// Enable features
_fnAddOptionsHtml( settings );
_fnSortInit( settings );

_colGroup( settings );

/* Okay to show that something is going on now */
_fnProcessingDisplay( settings, true );

_fnCallbackFire( settings, null, 'preInit', [settings], true );

// If there is default sorting required - let's do it. The sort function
// will do the drawing for us. Otherwise we draw the table regardless of the
// Ajax source - this allows the table to look initialised for Ajax sourcing
// data (show 'loading' message possibly)
_fnReDraw( settings );

var dataSrc = _fnDataSource( settings );

// Server-side processing init complete is done by _fnAjaxUpdateDraw
if ( dataSrc != 'ssp' ) {
// if there is an ajax source load the data
if ( dataSrc == 'ajax' ) {
_fnBuildAjax( settings, {}, function(json) {
var aData = _fnAjaxDataSrc( settings, json );

// Got the data - add it to the table
for ( i=0 ; i<aData.length ; i++ ) {
_fnAddData( settings, aData[i] );
}

// Reset the init display for cookie saving. We've already done
// a filter, and therefore cleared it before. So we need to make
// it appear 'fresh'
settings.iInitDisplayStart = iAjaxStart;
// Load the table's state (if needed) and then render around it and draw draw
_fnLoadState( settings, init, function () {
// Local data load
// Check if there is data passing into the constructor
if ( init.aaData ) {
for ( i=0 ; i<init.aaData.length ; i++ ) {
_fnAddData( settings, init.aaData[ i ] );
}
}
else if ( dataSrc == 'dom' ) {
// Grab the data from the page
_fnAddTr( settings, $(settings.nTBody).children('tr') );
}

_fnReDraw( settings );
_fnProcessingDisplay( settings, false );
// Enable features
_fnAddOptionsHtml( settings );
_fnSortInit( settings );

_colGroup( settings );

/* Okay to show that something is going on now */
_fnProcessingDisplay( settings, true );

_fnCallbackFire( settings, null, 'preInit', [settings], true );

// If there is default sorting required - let's do it. The sort function
// will do the drawing for us. Otherwise we draw the table regardless of the
// Ajax source - this allows the table to look initialised for Ajax sourcing
// data (show 'loading' message possibly)
_fnReDraw( settings );

// Server-side processing init complete is done by _fnAjaxUpdateDraw
if ( dataSrc != 'ssp' ) {
// if there is an ajax source load the data
if ( dataSrc == 'ajax' ) {
_fnBuildAjax( settings, {}, function(json) {
var aData = _fnAjaxDataSrc( settings, json );

// Got the data - add it to the table
for ( i=0 ; i<aData.length ; i++ ) {
_fnAddData( settings, aData[i] );
}

// Reset the init display for cookie saving. We've already done
// a filter, and therefore cleared it before. So we need to make
// it appear 'fresh'
settings.iInitDisplayStart = iAjaxStart;

_fnReDraw( settings );
_fnProcessingDisplay( settings, false );
_fnInitComplete( settings );
}, settings );
}
else {
_fnInitComplete( settings );
}, settings );
}
else {
_fnInitComplete( settings );
_fnProcessingDisplay( settings, false );
_fnProcessingDisplay( settings, false );
}
}
}
} );
}


Expand Down

0 comments on commit d802465

Please sign in to comment.