1 var i=0, iLen, j, jLen, k, kLen;
2 var sId = this.getAttribute( 'id' );
3 var bInitHandedOff = false;
4 var bUsePassedData = false;
8 if ( this.nodeName.toLowerCase() != 'table' )
10 _fnLog( null, 0, "Attempted to initialise DataTables on a node which is not a "+
11 "table: "+this.nodeName );
15 /* Check to see if we are re-initialising a table */
16 for ( i=0, iLen=DataTable.settings.length ; i<iLen ; i++ )
18 /* Base check on table node */
19 if ( DataTable.settings[i].nTable == this )
21 if ( oInit === undefined || oInit.bRetrieve )
23 return DataTable.settings[i].oInstance;
25 else if ( oInit.bDestroy )
27 DataTable.settings[i].oInstance.fnDestroy();
32 _fnLog( DataTable.settings[i], 0, "Cannot reinitialise DataTable.\n\n"+
33 "To retrieve the DataTables object for this table, pass no arguments or see "+
34 "the docs for bRetrieve and bDestroy" );
39 /* If the element we are initialising has the same ID as a table which was previously
40 * initialised, but the table nodes don't match (from before) then we destroy the old
41 * instance by simply deleting it. This is under the assumption that the table has been
42 * destroyed by other methods. Anyone using non-id selectors will need to do this manually
44 if ( DataTable.settings[i].sTableId == this.id )
46 DataTable.settings.splice( i, 1 );
51 /* Ensure the table has an ID - required for accessibility */
52 if ( sId === null || sId === "" )
54 sId = "DataTables_Table_"+(DataTable.ext._oExternConfig.iNextUnique++);
58 /* Create the settings object for this table and set some of the default parameters */
59 var oSettings = $.extend( true, {}, DataTable.models.oSettings, {
63 "sDestroyWidth": $(this).width(),
67 DataTable.settings.push( oSettings );
69 // Need to add the instance after the instance after the settings object has been added
70 // to the settings array, so we can self reference the table instance if more than one
71 oSettings.oInstance = (_that.length===1) ? _that : $(this).dataTable();
73 /* Setting up the initialisation object */
79 // Backwards compatibility, before we apply all the defaults
80 if ( oInit.oLanguage )
82 _fnLanguageCompat( oInit.oLanguage );
85 oInit = _fnExtend( $.extend(true, {}, DataTable.defaults), oInit );
87 // Map the initialisation options onto the settings object
88 _fnMap( oSettings.oFeatures, oInit, "bPaginate" );
89 _fnMap( oSettings.oFeatures, oInit, "bLengthChange" );
90 _fnMap( oSettings.oFeatures, oInit, "bFilter" );
91 _fnMap( oSettings.oFeatures, oInit, "bSort" );
92 _fnMap( oSettings.oFeatures, oInit, "bInfo" );
93 _fnMap( oSettings.oFeatures, oInit, "bProcessing" );
94 _fnMap( oSettings.oFeatures, oInit, "bAutoWidth" );
95 _fnMap( oSettings.oFeatures, oInit, "bSortClasses" );
96 _fnMap( oSettings.oFeatures, oInit, "bServerSide" );
97 _fnMap( oSettings.oFeatures, oInit, "bDeferRender" );
98 _fnMap( oSettings.oScroll, oInit, "sScrollX", "sX" );
99 _fnMap( oSettings.oScroll, oInit, "sScrollXInner", "sXInner" );
100 _fnMap( oSettings.oScroll, oInit, "sScrollY", "sY" );
101 _fnMap( oSettings.oScroll, oInit, "bScrollCollapse", "bCollapse" );
102 _fnMap( oSettings.oScroll, oInit, "bScrollInfinite", "bInfinite" );
103 _fnMap( oSettings.oScroll, oInit, "iScrollLoadGap", "iLoadGap" );
104 _fnMap( oSettings.oScroll, oInit, "bScrollAutoCss", "bAutoCss" );
105 _fnMap( oSettings, oInit, "asStripeClasses" );
106 _fnMap( oSettings, oInit, "asStripClasses", "asStripeClasses" ); // legacy
107 _fnMap( oSettings, oInit, "fnServerData" );
108 _fnMap( oSettings, oInit, "fnFormatNumber" );
109 _fnMap( oSettings, oInit, "sServerMethod" );
110 _fnMap( oSettings, oInit, "aaSorting" );
111 _fnMap( oSettings, oInit, "aaSortingFixed" );
112 _fnMap( oSettings, oInit, "aLengthMenu" );
113 _fnMap( oSettings, oInit, "sPaginationType" );
114 _fnMap( oSettings, oInit, "sAjaxSource" );
115 _fnMap( oSettings, oInit, "sAjaxDataProp" );
116 _fnMap( oSettings, oInit, "iCookieDuration" );
117 _fnMap( oSettings, oInit, "sCookiePrefix" );
118 _fnMap( oSettings, oInit, "sDom" );
119 _fnMap( oSettings, oInit, "bSortCellsTop" );
120 _fnMap( oSettings, oInit, "iTabIndex" );
121 _fnMap( oSettings, oInit, "oSearch", "oPreviousSearch" );
122 _fnMap( oSettings, oInit, "aoSearchCols", "aoPreSearchCols" );
123 _fnMap( oSettings, oInit, "iDisplayLength", "_iDisplayLength" );
124 _fnMap( oSettings, oInit, "bJQueryUI", "bJUI" );
125 _fnMap( oSettings, oInit, "fnCookieCallback" );
126 _fnMap( oSettings, oInit, "fnStateLoad" );
127 _fnMap( oSettings, oInit, "fnStateSave" );
128 _fnMap( oSettings.oLanguage, oInit, "fnInfoCallback" );
130 /* Callback functions which are array driven */
131 _fnCallbackReg( oSettings, 'aoDrawCallback', oInit.fnDrawCallback, 'user' );
132 _fnCallbackReg( oSettings, 'aoServerParams', oInit.fnServerParams, 'user' );
133 _fnCallbackReg( oSettings, 'aoStateSaveParams', oInit.fnStateSaveParams, 'user' );
134 _fnCallbackReg( oSettings, 'aoStateLoadParams', oInit.fnStateLoadParams, 'user' );
135 _fnCallbackReg( oSettings, 'aoStateLoaded', oInit.fnStateLoaded, 'user' );
136 _fnCallbackReg( oSettings, 'aoRowCallback', oInit.fnRowCallback, 'user' );
137 _fnCallbackReg( oSettings, 'aoRowCreatedCallback', oInit.fnCreatedRow, 'user' );
138 _fnCallbackReg( oSettings, 'aoHeaderCallback', oInit.fnHeaderCallback, 'user' );
139 _fnCallbackReg( oSettings, 'aoFooterCallback', oInit.fnFooterCallback, 'user' );
140 _fnCallbackReg( oSettings, 'aoInitComplete', oInit.fnInitComplete, 'user' );
141 _fnCallbackReg( oSettings, 'aoPreDrawCallback', oInit.fnPreDrawCallback, 'user' );
143 if ( oSettings.oFeatures.bServerSide && oSettings.oFeatures.bSort &&
144 oSettings.oFeatures.bSortClasses )
146 /* Enable sort classes for server-side processing. Safe to do it here, since server-side
147 * processing must be enabled by the developer
149 _fnCallbackReg( oSettings, 'aoDrawCallback', _fnSortingClasses, 'server_side_sort_classes' );
151 else if ( oSettings.oFeatures.bDeferRender )
153 _fnCallbackReg( oSettings, 'aoDrawCallback', _fnSortingClasses, 'defer_sort_classes' );
156 if ( oInit.bJQueryUI )
158 /* Use the JUI classes object for display. You could clone the oStdClasses object if
159 * you want to have multiple tables with multiple independent classes
161 $.extend( oSettings.oClasses, DataTable.ext.oJUIClasses );
163 if ( oInit.sDom === DataTable.defaults.sDom && DataTable.defaults.sDom === "lfrtip" )
165 /* Set the DOM to use a layout suitable for jQuery UI's theming */
166 oSettings.sDom = '<"H"lfr>t<"F"ip>';
171 $.extend( oSettings.oClasses, DataTable.ext.oStdClasses );
173 $(this).addClass( oSettings.oClasses.sTable );
175 /* Calculate the scroll bar width and cache it for use later on */
176 if ( oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "" )
178 oSettings.oScroll.iBarWidth = _fnScrollBarWidth();
181 if ( oSettings.iInitDisplayStart === undefined )
183 /* Display start point, taking into account the save saving */
184 oSettings.iInitDisplayStart = oInit.iDisplayStart;
185 oSettings._iDisplayStart = oInit.iDisplayStart;
188 /* Must be done after everything which can be overridden by a cookie! */
189 if ( oInit.bStateSave )
191 oSettings.oFeatures.bStateSave = true;
192 _fnLoadState( oSettings, oInit );
193 _fnCallbackReg( oSettings, 'aoDrawCallback', _fnSaveState, 'state_save' );
196 if ( oInit.iDeferLoading !== null )
198 oSettings.bDeferLoading = true;
199 var tmp = $.isArray( oInit.iDeferLoading );
200 oSettings._iRecordsDisplay = tmp ? oInit.iDeferLoading[0] : oInit.iDeferLoading;
201 oSettings._iRecordsTotal = tmp ? oInit.iDeferLoading[1] : oInit.iDeferLoading;
204 if ( oInit.aaData !== null )
206 bUsePassedData = true;
209 /* Language definitions */
210 if ( oInit.oLanguage.sUrl !== "" )
212 /* Get the language definitions from a file - because this Ajax call makes the language
213 * get async to the remainder of this function we use bInitHandedOff to indicate that
214 * _fnInitialise will be fired by the returned Ajax handler, rather than the constructor
216 oSettings.oLanguage.sUrl = oInit.oLanguage.sUrl;
217 $.getJSON( oSettings.oLanguage.sUrl, null, function( json ) {
218 _fnLanguageCompat( json );
219 $.extend( true, oSettings.oLanguage, oInit.oLanguage, json );
220 _fnInitialise( oSettings );
222 bInitHandedOff = true;
226 $.extend( true, oSettings.oLanguage, oInit.oLanguage );
233 if ( oInit.asStripeClasses === null )
235 oSettings.asStripeClasses =[
236 oSettings.oClasses.sStripeOdd,
237 oSettings.oClasses.sStripeEven
241 /* Remove row stripe classes if they are already on the table row */
242 iLen=oSettings.asStripeClasses.length;
243 oSettings.asDestroyStripes = [];
246 var bStripeRemove = false;
247 var anRows = $(this).children('tbody').children('tr:lt(' + iLen + ')');
248 for ( i=0 ; i<iLen ; i++ )
250 if ( anRows.hasClass( oSettings.asStripeClasses[i] ) )
252 bStripeRemove = true;
254 /* Store the classes which we are about to remove so they can be re-added on destroy */
255 oSettings.asDestroyStripes.push( oSettings.asStripeClasses[i] );
261 anRows.removeClass( oSettings.asStripeClasses.join(' ') );
267 * See if we should load columns automatically or use defined ones
271 var nThead = this.getElementsByTagName('thead');
272 if ( nThead.length !== 0 )
274 _fnDetectHeader( oSettings.aoHeader, nThead[0] );
275 anThs = _fnGetUniqueThs( oSettings );
278 /* If not given a column array, generate one with nulls */
279 if ( oInit.aoColumns === null )
282 for ( i=0, iLen=anThs.length ; i<iLen ; i++ )
284 aoColumnsInit.push( null );
289 aoColumnsInit = oInit.aoColumns;
292 /* Add the columns */
293 for ( i=0, iLen=aoColumnsInit.length ; i<iLen ; i++ )
295 /* Short cut - use the loop to check if we have column visibility state to restore */
296 if ( oInit.saved_aoColumns !== undefined && oInit.saved_aoColumns.length == iLen )
298 if ( aoColumnsInit[i] === null )
300 aoColumnsInit[i] = {};
302 aoColumnsInit[i].bVisible = oInit.saved_aoColumns[i].bVisible;
305 _fnAddColumn( oSettings, anThs ? anThs[i] : null );
308 /* Apply the column definitions */
309 _fnApplyColumnDefs( oSettings, oInit.aoColumnDefs, aoColumnsInit, function (iCol, oDef) {
310 _fnColumnOptions( oSettings, iCol, oDef );
316 * Check the aaSorting array
318 for ( i=0, iLen=oSettings.aaSorting.length ; i<iLen ; i++ )
320 if ( oSettings.aaSorting[i][0] >= oSettings.aoColumns.length )
322 oSettings.aaSorting[i][0] = 0;
324 var oColumn = oSettings.aoColumns[ oSettings.aaSorting[i][0] ];
326 /* Add a default sorting index */
327 if ( oSettings.aaSorting[i][2] === undefined )
329 oSettings.aaSorting[i][2] = 0;
332 /* If aaSorting is not defined, then we use the first indicator in asSorting */
333 if ( oInit.aaSorting === undefined && oSettings.saved_aaSorting === undefined )
335 oSettings.aaSorting[i][1] = oColumn.asSorting[0];
338 /* Set the current sorting index based on aoColumns.asSorting */
339 for ( j=0, jLen=oColumn.asSorting.length ; j<jLen ; j++ )
341 if ( oSettings.aaSorting[i][1] == oColumn.asSorting[j] )
343 oSettings.aaSorting[i][2] = j;
349 /* Do a first pass on the sorting classes (allows any size changes to be taken into
350 * account, and also will apply sorting disabled classes if disabled
352 _fnSortingClasses( oSettings );
357 * Cache the header, body and footer as required, creating them if needed
360 /* Browser support detection */
361 _fnBrowserDetect( oSettings );
363 // Work around for Webkit bug 83867 - store the caption-side before removing from doc
364 var captions = $(this).children('caption').each( function () {
365 this._captionSide = $(this).css('caption-side');
368 var thead = $(this).children('thead');
369 if ( thead.length === 0 )
371 thead = [ document.createElement( 'thead' ) ];
372 this.appendChild( thead[0] );
374 oSettings.nTHead = thead[0];
376 var tbody = $(this).children('tbody');
377 if ( tbody.length === 0 )
379 tbody = [ document.createElement( 'tbody' ) ];
380 this.appendChild( tbody[0] );
382 oSettings.nTBody = tbody[0];
383 oSettings.nTBody.setAttribute( "role", "alert" );
384 oSettings.nTBody.setAttribute( "aria-live", "polite" );
385 oSettings.nTBody.setAttribute( "aria-relevant", "all" );
387 var tfoot = $(this).children('tfoot');
388 if ( tfoot.length === 0 && captions.length > 0 && (oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "") )
390 // If we are a scrolling table, and no footer has been given, then we need to create
391 // a tfoot element for the caption element to be appended to
392 tfoot = [ document.createElement( 'tfoot' ) ];
393 this.appendChild( tfoot[0] );
396 if ( tfoot.length > 0 )
398 oSettings.nTFoot = tfoot[0];
399 _fnDetectHeader( oSettings.aoFooter, oSettings.nTFoot );
402 /* Check if there is data passing into the constructor */
403 if ( bUsePassedData )
405 for ( i=0 ; i<oInit.aaData.length ; i++ )
407 _fnAddData( oSettings, oInit.aaData[ i ] );
412 /* Grab the data from the page */
413 _fnGatherData( oSettings );
416 /* Copy the data index array */
417 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
419 /* Initialisation complete - table can be drawn */
420 oSettings.bInitialised = true;
422 /* Check if we need to initialise the table (it might not have been handed off to the
423 * language processor)
425 if ( bInitHandedOff === false )
427 _fnInitialise( oSettings );