2 * Add a data array to the table, creating DOM node etc. This is the parallel to
3 * _fnGatherData, but for adding rows from a Javascript source, rather than a
5 * @param {object} oSettings dataTables settings object
6 * @param {array} aData data array to be added
7 * @returns {int} >=0 if successful (index of new aoData entry), -1 if failed
8 * @memberof DataTable#oApi
10 function _fnAddData ( oSettings, aDataSupplied )
14 /* Take an independent copy of the data source so we can bash it about as we wish */
15 var aDataIn = ($.isArray(aDataSupplied)) ?
16 aDataSupplied.slice() :
17 $.extend( true, {}, aDataSupplied );
19 /* Create the object for storing information about this new row */
20 var iRow = oSettings.aoData.length;
21 var oData = $.extend( true, {}, DataTable.models.oRow );
22 oData._aData = aDataIn;
23 oSettings.aoData.push( oData );
25 /* Create the cells */
27 for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
29 oCol = oSettings.aoColumns[i];
31 /* Use rendered data for filtering / sorting */
32 if ( typeof oCol.fnRender === 'function' && oCol.bUseRendered && oCol.mData !== null )
34 _fnSetCellData( oSettings, iRow, i, _fnRender(oSettings, iRow, i) );
38 _fnSetCellData( oSettings, iRow, i, _fnGetCellData( oSettings, iRow, i ) );
41 /* See if we should auto-detect the column type */
42 if ( oCol._bAutoType && oCol.sType != 'string' )
44 /* Attempt to auto detect the type - same as _fnGatherData() */
45 var sVarType = _fnGetCellData( oSettings, iRow, i, 'type' );
46 if ( sVarType !== null && sVarType !== '' )
48 sThisType = _fnDetectType( sVarType );
49 if ( oCol.sType === null )
51 oCol.sType = sThisType;
53 else if ( oCol.sType != sThisType && oCol.sType != "html" )
55 /* String is always the 'fallback' option */
56 oCol.sType = 'string';
62 /* Add to the display array */
63 oSettings.aiDisplayMaster.push( iRow );
65 /* Create the DOM information */
66 if ( !oSettings.oFeatures.bDeferRender )
68 _fnCreateTr( oSettings, iRow );
76 * Read in the data from the target table from the DOM
77 * @param {object} oSettings dataTables settings object
78 * @memberof DataTable#oApi
80 function _fnGatherData( oSettings )
82 var iLoop, i, iLen, j, jLen, jInner,
83 nTds, nTrs, nTd, nTr, aLocalData, iThisIndex,
84 iRow, iRows, iColumn, iColumns, sNodeName,
88 * Process by row first
89 * Add the data object for the whole table - storing the tr node. Note - no point in getting
90 * DOM based data if we are going to go and replace it with Ajax source data.
92 if ( oSettings.bDeferLoading || oSettings.sAjaxSource === null )
94 nTr = oSettings.nTBody.firstChild;
97 if ( nTr.nodeName.toUpperCase() == "TR" )
99 iThisIndex = oSettings.aoData.length;
100 nTr._DT_RowIndex = iThisIndex;
101 oSettings.aoData.push( $.extend( true, {}, DataTable.models.oRow, {
105 oSettings.aiDisplayMaster.push( iThisIndex );
106 nTd = nTr.firstChild;
110 sNodeName = nTd.nodeName.toUpperCase();
111 if ( sNodeName == "TD" || sNodeName == "TH" )
113 _fnSetCellData( oSettings, iThisIndex, jInner, $.trim(nTd.innerHTML) );
116 nTd = nTd.nextSibling;
119 nTr = nTr.nextSibling;
123 /* Gather in the TD elements of the Table - note that this is basically the same as
124 * fnGetTdNodes, but that function takes account of hidden columns, which we haven't yet
127 nTrs = _fnGetTrNodes( oSettings );
129 for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
131 nTd = nTrs[i].firstChild;
134 sNodeName = nTd.nodeName.toUpperCase();
135 if ( sNodeName == "TD" || sNodeName == "TH" )
139 nTd = nTd.nextSibling;
143 /* Now process by column */
144 for ( iColumn=0, iColumns=oSettings.aoColumns.length ; iColumn<iColumns ; iColumn++ )
146 oCol = oSettings.aoColumns[iColumn];
148 /* Get the title of the column - unless there is a user set one */
149 if ( oCol.sTitle === null )
151 oCol.sTitle = oCol.nTh.innerHTML;
155 bAutoType = oCol._bAutoType,
156 bRender = typeof oCol.fnRender === 'function',
157 bClass = oCol.sClass !== null,
158 bVisible = oCol.bVisible,
159 nCell, sThisType, sRendered, sValType;
161 /* A single loop to rule them all (and be more efficient) */
162 if ( bAutoType || bRender || bClass || !bVisible )
164 for ( iRow=0, iRows=oSettings.aoData.length ; iRow<iRows ; iRow++ )
166 oData = oSettings.aoData[iRow];
167 nCell = nTds[ (iRow*iColumns) + iColumn ];
170 if ( bAutoType && oCol.sType != 'string' )
172 sValType = _fnGetCellData( oSettings, iRow, iColumn, 'type' );
173 if ( sValType !== '' )
175 sThisType = _fnDetectType( sValType );
176 if ( oCol.sType === null )
178 oCol.sType = sThisType;
180 else if ( oCol.sType != sThisType &&
181 oCol.sType != "html" )
183 /* String is always the 'fallback' option */
184 oCol.sType = 'string';
191 // mRender has been defined, so we need to get the value and set it
192 nCell.innerHTML = _fnGetCellData( oSettings, iRow, iColumn, 'display' );
194 else if ( oCol.mData !== iColumn )
196 // If mData is not the same as the column number, then we need to
197 // get the dev set value. If it is the column, no point in wasting
198 // time setting the value that is already there!
199 nCell.innerHTML = _fnGetCellData( oSettings, iRow, iColumn, 'display' );
205 sRendered = _fnRender( oSettings, iRow, iColumn );
206 nCell.innerHTML = sRendered;
207 if ( oCol.bUseRendered )
209 /* Use the rendered data for filtering / sorting */
210 _fnSetCellData( oSettings, iRow, iColumn, sRendered );
217 nCell.className += ' '+oCol.sClass;
220 /* Column visibility */
223 oData._anHidden[iColumn] = nCell;
224 nCell.parentNode.removeChild( nCell );
228 oData._anHidden[iColumn] = null;
231 if ( oCol.fnCreatedCell )
233 oCol.fnCreatedCell.call( oSettings.oInstance,
234 nCell, _fnGetCellData( oSettings, iRow, iColumn, 'display' ), oData._aData, iRow, iColumn
241 /* Row created callbacks */
242 if ( oSettings.aoRowCreatedCallback.length !== 0 )
244 for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
246 oData = oSettings.aoData[i];
247 _fnCallbackFire( oSettings, 'aoRowCreatedCallback', null, [oData.nTr, oData._aData, i] );
254 * Take a TR element and convert it to an index in aoData
255 * @param {object} oSettings dataTables settings object
256 * @param {node} n the TR element to find
257 * @returns {int} index if the node is found, null if not
258 * @memberof DataTable#oApi
260 function _fnNodeToDataIndex( oSettings, n )
262 return (n._DT_RowIndex!==undefined) ? n._DT_RowIndex : null;
267 * Take a TD element and convert it into a column data index (not the visible index)
268 * @param {object} oSettings dataTables settings object
269 * @param {int} iRow The row number the TD/TH can be found in
270 * @param {node} n The TD/TH element to find
271 * @returns {int} index if the node is found, -1 if not
272 * @memberof DataTable#oApi
274 function _fnNodeToColumnIndex( oSettings, iRow, n )
276 var anCells = _fnGetTdNodes( oSettings, iRow );
278 for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
280 if ( anCells[i] === n )
290 * Get an array of data for a given row from the internal data cache
291 * @param {object} oSettings dataTables settings object
292 * @param {int} iRow aoData row id
293 * @param {string} sSpecific data get type ('type' 'filter' 'sort')
294 * @param {array} aiColumns Array of column indexes to get data from
295 * @returns {array} Data array
296 * @memberof DataTable#oApi
298 function _fnGetRowData( oSettings, iRow, sSpecific, aiColumns )
301 for ( var i=0, iLen=aiColumns.length ; i<iLen ; i++ )
303 out.push( _fnGetCellData( oSettings, iRow, aiColumns[i], sSpecific ) );
310 * Get the data for a given cell from the internal cache, taking into account data mapping
311 * @param {object} oSettings dataTables settings object
312 * @param {int} iRow aoData row id
313 * @param {int} iCol Column index
314 * @param {string} sSpecific data get type ('display', 'type' 'filter' 'sort')
315 * @returns {*} Cell data
316 * @memberof DataTable#oApi
318 function _fnGetCellData( oSettings, iRow, iCol, sSpecific )
321 var oCol = oSettings.aoColumns[iCol];
322 var oData = oSettings.aoData[iRow]._aData;
324 if ( (sData=oCol.fnGetData( oData, sSpecific )) === undefined )
326 if ( oSettings.iDrawError != oSettings.iDraw && oCol.sDefaultContent === null )
328 _fnLog( oSettings, 0, "Requested unknown parameter "+
329 (typeof oCol.mData=='function' ? '{mData function}' : "'"+oCol.mData+"'")+
330 " from the data source for row "+iRow );
331 oSettings.iDrawError = oSettings.iDraw;
333 return oCol.sDefaultContent;
336 /* When the data source is null, we can use default column data */
337 if ( sData === null && oCol.sDefaultContent !== null )
339 sData = oCol.sDefaultContent;
341 else if ( typeof sData === 'function' )
343 /* If the data source is a function, then we run it and use the return */
347 if ( sSpecific == 'display' && sData === null )
356 * Set the value for a specific cell, into the internal data cache
357 * @param {object} oSettings dataTables settings object
358 * @param {int} iRow aoData row id
359 * @param {int} iCol Column index
360 * @param {*} val Value to set
361 * @memberof DataTable#oApi
363 function _fnSetCellData( oSettings, iRow, iCol, val )
365 var oCol = oSettings.aoColumns[iCol];
366 var oData = oSettings.aoData[iRow]._aData;
368 oCol.fnSetData( oData, val );
372 // Private variable that is used to match array syntax in the data property object
373 var __reArray = /\[.*?\]$/;
376 * Return a function that can be used to get data from a source object, taking
377 * into account the ability to use nested objects as a source
378 * @param {string|int|function} mSource The data source for the object
379 * @returns {function} Data get function
380 * @memberof DataTable#oApi
382 function _fnGetObjectDataFn( mSource )
384 if ( mSource === null )
386 /* Give an empty string for rendering / sorting etc */
387 return function (data, type) {
391 else if ( typeof mSource === 'function' )
393 return function (data, type, extra) {
394 return mSource( data, type, extra );
397 else if ( typeof mSource === 'string' && (mSource.indexOf('.') !== -1 || mSource.indexOf('[') !== -1) )
399 /* If there is a . in the source string then the data source is in a
400 * nested object so we loop over the data for each level to get the next
401 * level down. On each loop we test for undefined, and if found immediately
402 * return. This allows entire objects to be missing and sDefaultContent to
403 * be used if defined, rather than throwing an error
405 var fetchData = function (data, type, src) {
406 var a = src.split('.');
407 var arrayNotation, out, innerSrc;
411 for ( var i=0, iLen=a.length ; i<iLen ; i++ )
413 // Check if we are dealing with an array notation request
414 arrayNotation = a[i].match(__reArray);
416 if ( arrayNotation ) {
417 a[i] = a[i].replace(__reArray, '');
419 // Condition allows simply [] to be passed in
425 // Get the remainder of the nested object to get
427 innerSrc = a.join('.');
429 // Traverse each entry in the array getting the properties requested
430 for ( var j=0, jLen=data.length ; j<jLen ; j++ ) {
431 out.push( fetchData( data[j], type, innerSrc ) );
434 // If a string is given in between the array notation indicators, that
435 // is used to join the strings together, otherwise an array is returned
436 var join = arrayNotation[0].substring(1, arrayNotation[0].length-1);
437 data = (join==="") ? out : out.join(join);
439 // The inner call to fetchData has already traversed through the remainder
440 // of the source requested, so we exit from the loop
444 if ( data === null || data[ a[i] ] === undefined )
455 return function (data, type) {
456 return fetchData( data, type, mSource );
461 /* Array or flat object mapping */
462 return function (data, type) {
463 return data[mSource];
470 * Return a function that can be used to set data from a source object, taking
471 * into account the ability to use nested objects as a source
472 * @param {string|int|function} mSource The data source for the object
473 * @returns {function} Data set function
474 * @memberof DataTable#oApi
476 function _fnSetObjectDataFn( mSource )
478 if ( mSource === null )
480 /* Nothing to do when the data source is null */
481 return function (data, val) {};
483 else if ( typeof mSource === 'function' )
485 return function (data, val) {
486 mSource( data, 'set', val );
489 else if ( typeof mSource === 'string' && (mSource.indexOf('.') !== -1 || mSource.indexOf('[') !== -1) )
491 /* Like the get, we need to get data from a nested object */
492 var setData = function (data, val, src) {
493 var a = src.split('.'), b;
494 var arrayNotation, o, innerSrc;
496 for ( var i=0, iLen=a.length-1 ; i<iLen ; i++ )
498 // Check if we are dealing with an array notation request
499 arrayNotation = a[i].match(__reArray);
503 a[i] = a[i].replace(__reArray, '');
506 // Get the remainder of the nested object to set so we can recurse
509 innerSrc = b.join('.');
511 // Traverse each entry in the array setting the properties requested
512 for ( var j=0, jLen=val.length ; j<jLen ; j++ )
515 setData( o, val[j], innerSrc );
516 data[ a[i] ].push( o );
519 // The inner call to setData has already traversed through the remainder
520 // of the source and has set the data, thus we can exit here
524 // If the nested object doesn't currently exist - since we are
525 // trying to set the value - create it
526 if ( data[ a[i] ] === null || data[ a[i] ] === undefined )
533 // If array notation is used, we just want to strip it and use the property name
534 // and assign the value. If it isn't used, then we get the result we want anyway
535 data[ a[a.length-1].replace(__reArray, '') ] = val;
538 return function (data, val) {
539 return setData( data, val, mSource );
544 /* Array or flat object mapping */
545 return function (data, val) {
553 * Return an array with the full table data
554 * @param {object} oSettings dataTables settings object
555 * @returns array {array} aData Master data array
556 * @memberof DataTable#oApi
558 function _fnGetDataMaster ( oSettings )
561 var iLen = oSettings.aoData.length;
562 for ( var i=0 ; i<iLen; i++ )
564 aData.push( oSettings.aoData[i]._aData );
572 * @param {object} oSettings dataTables settings object
573 * @memberof DataTable#oApi
575 function _fnClearTable( oSettings )
577 oSettings.aoData.splice( 0, oSettings.aoData.length );
578 oSettings.aiDisplayMaster.splice( 0, oSettings.aiDisplayMaster.length );
579 oSettings.aiDisplay.splice( 0, oSettings.aiDisplay.length );
580 _fnCalculateEnd( oSettings );
585 * Take an array of integers (index array) and remove a target integer (value - not
587 * @param {array} a Index array to target
588 * @param {int} iTarget value to find
589 * @memberof DataTable#oApi
591 function _fnDeleteIndex( a, iTarget )
593 var iTargetIndex = -1;
595 for ( var i=0, iLen=a.length ; i<iLen ; i++ )
597 if ( a[i] == iTarget )
601 else if ( a[i] > iTarget )
607 if ( iTargetIndex != -1 )
609 a.splice( iTargetIndex, 1 );
615 * Call the developer defined fnRender function for a given cell (row/column) with
616 * the required parameters and return the result.
617 * @param {object} oSettings dataTables settings object
618 * @param {int} iRow aoData index for the row
619 * @param {int} iCol aoColumns index for the column
620 * @returns {*} Return of the developer's fnRender function
621 * @memberof DataTable#oApi
623 function _fnRender( oSettings, iRow, iCol )
625 var oCol = oSettings.aoColumns[iCol];
627 return oCol.fnRender( {
630 "oSettings": oSettings,
631 "aData": oSettings.aoData[iRow]._aData,
632 "mDataProp": oCol.mData
633 }, _fnGetCellData(oSettings, iRow, iCol, 'display') );