2 * Create a new TR element (and it's TD children) for a row
3 * @param {object} oSettings dataTables settings object
4 * @param {int} iRow Row to consider
5 * @memberof DataTable#oApi
7 function _fnCreateTr ( oSettings, iRow )
9 var oData = oSettings.aoData[iRow];
12 if ( oData.nTr === null )
14 oData.nTr = document.createElement('tr');
16 /* Use a private property on the node to allow reserve mapping from the node
17 * to the aoData array for fast look up
19 oData.nTr._DT_RowIndex = iRow;
21 /* Special parameters can be given by the data source to be used on the row */
22 if ( oData._aData.DT_RowId )
24 oData.nTr.id = oData._aData.DT_RowId;
27 if ( oData._aData.DT_RowClass )
29 oData.nTr.className = oData._aData.DT_RowClass;
32 /* Process each column */
33 for ( var i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
35 var oCol = oSettings.aoColumns[i];
36 nTd = document.createElement( oCol.sCellType );
38 /* Render if needed - if bUseRendered is true then we already have the rendered
39 * value in the data source - so can just use that
41 nTd.innerHTML = (typeof oCol.fnRender === 'function' && (!oCol.bUseRendered || oCol.mData === null)) ?
42 _fnRender( oSettings, iRow, i ) :
43 _fnGetCellData( oSettings, iRow, i, 'display' );
45 /* Add user defined class */
46 if ( oCol.sClass !== null )
48 nTd.className = oCol.sClass;
53 oData.nTr.appendChild( nTd );
54 oData._anHidden[i] = null;
58 oData._anHidden[i] = nTd;
61 if ( oCol.fnCreatedCell )
63 oCol.fnCreatedCell.call( oSettings.oInstance,
64 nTd, _fnGetCellData( oSettings, iRow, i, 'display' ), oData._aData, iRow, i
69 _fnCallbackFire( oSettings, 'aoRowCreatedCallback', null, [oData.nTr, oData._aData, iRow] );
75 * Create the HTML header for the table
76 * @param {object} oSettings dataTables settings object
77 * @memberof DataTable#oApi
79 function _fnBuildHead( oSettings )
81 var i, nTh, iLen, j, jLen;
82 var iThs = $('th, td', oSettings.nTHead).length;
86 /* If there is a header in place - then use it - otherwise it's going to get nuked... */
89 /* We've got a thead from the DOM, so remove hidden columns and apply width to vis cols */
90 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
92 nTh = oSettings.aoColumns[i].nTh;
93 nTh.setAttribute('role', 'columnheader');
94 if ( oSettings.aoColumns[i].bSortable )
96 nTh.setAttribute('tabindex', oSettings.iTabIndex);
97 nTh.setAttribute('aria-controls', oSettings.sTableId);
100 if ( oSettings.aoColumns[i].sClass !== null )
102 $(nTh).addClass( oSettings.aoColumns[i].sClass );
105 /* Set the title of the column if it is user defined (not what was auto detected) */
106 if ( oSettings.aoColumns[i].sTitle != nTh.innerHTML )
108 nTh.innerHTML = oSettings.aoColumns[i].sTitle;
114 /* We don't have a header in the DOM - so we are going to have to create one */
115 var nTr = document.createElement( "tr" );
117 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
119 nTh = oSettings.aoColumns[i].nTh;
120 nTh.innerHTML = oSettings.aoColumns[i].sTitle;
121 nTh.setAttribute('tabindex', '0');
123 if ( oSettings.aoColumns[i].sClass !== null )
125 $(nTh).addClass( oSettings.aoColumns[i].sClass );
128 nTr.appendChild( nTh );
130 $(oSettings.nTHead).html( '' )[0].appendChild( nTr );
131 _fnDetectHeader( oSettings.aoHeader, oSettings.nTHead );
134 /* ARIA role for the rows */
135 $(oSettings.nTHead).children('tr').attr('role', 'row');
137 /* Add the extra markup needed by jQuery UI's themes */
138 if ( oSettings.bJUI )
140 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
142 nTh = oSettings.aoColumns[i].nTh;
144 var nDiv = document.createElement('div');
145 nDiv.className = oSettings.oClasses.sSortJUIWrapper;
146 $(nTh).contents().appendTo(nDiv);
148 var nSpan = document.createElement('span');
149 nSpan.className = oSettings.oClasses.sSortIcon;
150 nDiv.appendChild( nSpan );
151 nTh.appendChild( nDiv );
155 if ( oSettings.oFeatures.bSort )
157 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
159 if ( oSettings.aoColumns[i].bSortable !== false )
161 _fnSortAttachListener( oSettings, oSettings.aoColumns[i].nTh, i );
165 $(oSettings.aoColumns[i].nTh).addClass( oSettings.oClasses.sSortableNone );
170 /* Deal with the footer - add classes if required */
171 if ( oSettings.oClasses.sFooterTH !== "" )
173 $(oSettings.nTFoot).children('tr').children('th').addClass( oSettings.oClasses.sFooterTH );
176 /* Cache the footer elements */
177 if ( oSettings.nTFoot !== null )
179 var anCells = _fnGetUniqueThs( oSettings, null, oSettings.aoFooter );
180 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
184 oSettings.aoColumns[i].nTf = anCells[i];
185 if ( oSettings.aoColumns[i].sClass )
187 $(anCells[i]).addClass( oSettings.aoColumns[i].sClass );
196 * Draw the header (or footer) element based on the column visibility states. The
197 * methodology here is to use the layout array from _fnDetectHeader, modified for
198 * the instantaneous column visibility, to construct the new layout. The grid is
199 * traversed over cell at a time in a rows x columns grid fashion, although each
200 * cell insert can cover multiple elements in the grid - which is tracks using the
201 * aApplied array. Cell inserts in the grid will only occur where there isn't
202 * already a cell in that position.
203 * @param {object} oSettings dataTables settings object
204 * @param array {objects} aoSource Layout array from _fnDetectHeader
205 * @param {boolean} [bIncludeHidden=false] If true then include the hidden columns in the calc,
206 * @memberof DataTable#oApi
208 function _fnDrawHead( oSettings, aoSource, bIncludeHidden )
210 var i, iLen, j, jLen, k, kLen, n, nLocalTr;
213 var iColumns = oSettings.aoColumns.length;
214 var iRowspan, iColspan;
216 if ( bIncludeHidden === undefined )
218 bIncludeHidden = false;
221 /* Make a copy of the master layout array, but without the visible columns in it */
222 for ( i=0, iLen=aoSource.length ; i<iLen ; i++ )
224 aoLocal[i] = aoSource[i].slice();
225 aoLocal[i].nTr = aoSource[i].nTr;
227 /* Remove any columns which are currently hidden */
228 for ( j=iColumns-1 ; j>=0 ; j-- )
230 if ( !oSettings.aoColumns[j].bVisible && !bIncludeHidden )
232 aoLocal[i].splice( j, 1 );
236 /* Prep the applied array - it needs an element for each row */
240 for ( i=0, iLen=aoLocal.length ; i<iLen ; i++ )
242 nLocalTr = aoLocal[i].nTr;
244 /* All cells are going to be replaced, so empty out the row */
247 while( (n = nLocalTr.firstChild) )
249 nLocalTr.removeChild( n );
253 for ( j=0, jLen=aoLocal[i].length ; j<jLen ; j++ )
258 /* Check to see if there is already a cell (row/colspan) covering our target
259 * insert point. If there is, then there is nothing to do.
261 if ( aApplied[i][j] === undefined )
263 nLocalTr.appendChild( aoLocal[i][j].cell );
266 /* Expand the cell to cover as many rows as needed */
267 while ( aoLocal[i+iRowspan] !== undefined &&
268 aoLocal[i][j].cell == aoLocal[i+iRowspan][j].cell )
270 aApplied[i+iRowspan][j] = 1;
274 /* Expand the cell to cover as many columns as needed */
275 while ( aoLocal[i][j+iColspan] !== undefined &&
276 aoLocal[i][j].cell == aoLocal[i][j+iColspan].cell )
278 /* Must update the applied array over the rows for the columns */
279 for ( k=0 ; k<iRowspan ; k++ )
281 aApplied[i+k][j+iColspan] = 1;
286 /* Do the actual expansion in the DOM */
287 aoLocal[i][j].cell.rowSpan = iRowspan;
288 aoLocal[i][j].cell.colSpan = iColspan;
296 * Insert the required TR nodes into the table for display
297 * @param {object} oSettings dataTables settings object
298 * @memberof DataTable#oApi
300 function _fnDraw( oSettings )
302 /* Provide a pre-callback function which can be used to cancel the draw is false is returned */
303 var aPreDraw = _fnCallbackFire( oSettings, 'aoPreDrawCallback', 'preDraw', [oSettings] );
304 if ( $.inArray( false, aPreDraw ) !== -1 )
306 _fnProcessingDisplay( oSettings, false );
313 var iStripes = oSettings.asStripeClasses.length;
314 var iOpenRows = oSettings.aoOpenRows.length;
316 oSettings.bDrawing = true;
318 /* Check and see if we have an initial draw position from state saving */
319 if ( oSettings.iInitDisplayStart !== undefined && oSettings.iInitDisplayStart != -1 )
321 if ( oSettings.oFeatures.bServerSide )
323 oSettings._iDisplayStart = oSettings.iInitDisplayStart;
327 oSettings._iDisplayStart = (oSettings.iInitDisplayStart >= oSettings.fnRecordsDisplay()) ?
328 0 : oSettings.iInitDisplayStart;
330 oSettings.iInitDisplayStart = -1;
331 _fnCalculateEnd( oSettings );
334 /* Server-side processing draw intercept */
335 if ( oSettings.bDeferLoading )
337 oSettings.bDeferLoading = false;
340 else if ( !oSettings.oFeatures.bServerSide )
344 else if ( !oSettings.bDestroying && !_fnAjaxUpdate( oSettings ) )
349 if ( oSettings.aiDisplay.length !== 0 )
351 var iStart = oSettings._iDisplayStart;
352 var iEnd = oSettings._iDisplayEnd;
354 if ( oSettings.oFeatures.bServerSide )
357 iEnd = oSettings.aoData.length;
360 for ( var j=iStart ; j<iEnd ; j++ )
362 var aoData = oSettings.aoData[ oSettings.aiDisplay[j] ];
363 if ( aoData.nTr === null )
365 _fnCreateTr( oSettings, oSettings.aiDisplay[j] );
368 var nRow = aoData.nTr;
370 /* Remove the old striping classes and then add the new one */
371 if ( iStripes !== 0 )
373 var sStripe = oSettings.asStripeClasses[ iRowCount % iStripes ];
374 if ( aoData._sRowStripe != sStripe )
376 $(nRow).removeClass( aoData._sRowStripe ).addClass( sStripe );
377 aoData._sRowStripe = sStripe;
381 /* Row callback functions - might want to manipulate the row */
382 _fnCallbackFire( oSettings, 'aoRowCallback', null,
383 [nRow, oSettings.aoData[ oSettings.aiDisplay[j] ]._aData, iRowCount, j] );
388 /* If there is an open row - and it is attached to this parent - attach it on redraw */
389 if ( iOpenRows !== 0 )
391 for ( var k=0 ; k<iOpenRows ; k++ )
393 if ( nRow == oSettings.aoOpenRows[k].nParent )
395 anRows.push( oSettings.aoOpenRows[k].nTr );
404 /* Table is empty - create a row with an empty message in it */
405 anRows[ 0 ] = document.createElement( 'tr' );
407 if ( oSettings.asStripeClasses[0] )
409 anRows[ 0 ].className = oSettings.asStripeClasses[0];
412 var oLang = oSettings.oLanguage;
413 var sZero = oLang.sZeroRecords;
414 if ( oSettings.iDraw == 1 && oSettings.sAjaxSource !== null && !oSettings.oFeatures.bServerSide )
416 sZero = oLang.sLoadingRecords;
418 else if ( oLang.sEmptyTable && oSettings.fnRecordsTotal() === 0 )
420 sZero = oLang.sEmptyTable;
423 var nTd = document.createElement( 'td' );
424 nTd.setAttribute( 'valign', "top" );
425 nTd.colSpan = _fnVisbleColumns( oSettings );
426 nTd.className = oSettings.oClasses.sRowEmpty;
427 nTd.innerHTML = _fnInfoMacros( oSettings, sZero );
429 anRows[ iRowCount ].appendChild( nTd );
432 /* Header and footer callbacks */
433 _fnCallbackFire( oSettings, 'aoHeaderCallback', 'header', [ $(oSettings.nTHead).children('tr')[0],
434 _fnGetDataMaster( oSettings ), oSettings._iDisplayStart, oSettings.fnDisplayEnd(), oSettings.aiDisplay ] );
436 _fnCallbackFire( oSettings, 'aoFooterCallback', 'footer', [ $(oSettings.nTFoot).children('tr')[0],
437 _fnGetDataMaster( oSettings ), oSettings._iDisplayStart, oSettings.fnDisplayEnd(), oSettings.aiDisplay ] );
440 * Need to remove any old row from the display - note we can't just empty the tbody using
441 * $().html('') since this will unbind the jQuery event handlers (even although the node
442 * still exists!) - equally we can't use innerHTML, since IE throws an exception.
445 nAddFrag = document.createDocumentFragment(),
446 nRemoveFrag = document.createDocumentFragment(),
449 if ( oSettings.nTBody )
451 nBodyPar = oSettings.nTBody.parentNode;
452 nRemoveFrag.appendChild( oSettings.nTBody );
454 /* When doing infinite scrolling, only remove child rows when sorting, filtering or start
455 * up. When not infinite scroll, always do it.
457 if ( !oSettings.oScroll.bInfinite || !oSettings._bInitComplete ||
458 oSettings.bSorted || oSettings.bFiltered )
460 while( (n = oSettings.nTBody.firstChild) )
462 oSettings.nTBody.removeChild( n );
466 /* Put the draw table into the dom */
467 for ( i=0, iLen=anRows.length ; i<iLen ; i++ )
469 nAddFrag.appendChild( anRows[i] );
472 oSettings.nTBody.appendChild( nAddFrag );
473 if ( nBodyPar !== null )
475 nBodyPar.appendChild( oSettings.nTBody );
479 /* Call all required callback functions for the end of a draw */
480 _fnCallbackFire( oSettings, 'aoDrawCallback', 'draw', [oSettings] );
482 /* Draw is complete, sorting and filtering must be as well */
483 oSettings.bSorted = false;
484 oSettings.bFiltered = false;
485 oSettings.bDrawing = false;
487 if ( oSettings.oFeatures.bServerSide )
489 _fnProcessingDisplay( oSettings, false );
490 if ( !oSettings._bInitComplete )
492 _fnInitComplete( oSettings );
499 * Redraw the table - taking account of the various features which are enabled
500 * @param {object} oSettings dataTables settings object
501 * @memberof DataTable#oApi
503 function _fnReDraw( oSettings )
505 if ( oSettings.oFeatures.bSort )
507 /* Sorting will refilter and draw for us */
508 _fnSort( oSettings, oSettings.oPreviousSearch );
510 else if ( oSettings.oFeatures.bFilter )
512 /* Filtering will redraw for us */
513 _fnFilterComplete( oSettings, oSettings.oPreviousSearch );
517 _fnCalculateEnd( oSettings );
518 _fnDraw( oSettings );
524 * Add the options to the page HTML for the table
525 * @param {object} oSettings dataTables settings object
526 * @memberof DataTable#oApi
528 function _fnAddOptionsHtml ( oSettings )
531 * Create a temporary, empty, div which we can later on replace with what we have generated
532 * we do it this way to rendering the 'options' html offline - speed :-)
534 var nHolding = $('<div></div>')[0];
535 oSettings.nTable.parentNode.insertBefore( nHolding, oSettings.nTable );
538 * All DataTables are wrapped in a div
540 oSettings.nTableWrapper = $('<div id="'+oSettings.sTableId+'_wrapper" class="'+oSettings.oClasses.sWrapper+'" role="grid"></div>')[0];
541 oSettings.nTableReinsertBefore = oSettings.nTable.nextSibling;
543 /* Track where we want to insert the option */
544 var nInsertNode = oSettings.nTableWrapper;
546 /* Loop over the user set positioning and place the elements as needed */
547 var aDom = oSettings.sDom.split('');
548 var nTmp, iPushFeature, cOption, nNewNode, cNext, sAttr, j;
549 for ( var i=0 ; i<aDom.length ; i++ )
554 if ( cOption == '<' )
556 /* New container div */
557 nNewNode = $('<div></div>')[0];
559 /* Check to see if we should append an id and/or a class name to the container */
561 if ( cNext == "'" || cNext == '"' )
565 while ( aDom[i+j] != cNext )
571 /* Replace jQuery UI constants */
574 sAttr = oSettings.oClasses.sJUIHeader;
576 else if ( sAttr == "F" )
578 sAttr = oSettings.oClasses.sJUIFooter;
581 /* The attribute can be in the format of "#id.class", "#id" or "class" This logic
582 * breaks the string into parts and applies them as needed
584 if ( sAttr.indexOf('.') != -1 )
586 var aSplit = sAttr.split('.');
587 nNewNode.id = aSplit[0].substr(1, aSplit[0].length-1);
588 nNewNode.className = aSplit[1];
590 else if ( sAttr.charAt(0) == "#" )
592 nNewNode.id = sAttr.substr(1, sAttr.length-1);
596 nNewNode.className = sAttr;
599 i += j; /* Move along the position array */
602 nInsertNode.appendChild( nNewNode );
603 nInsertNode = nNewNode;
605 else if ( cOption == '>' )
607 /* End container div */
608 nInsertNode = nInsertNode.parentNode;
610 else if ( cOption == 'l' && oSettings.oFeatures.bPaginate && oSettings.oFeatures.bLengthChange )
613 nTmp = _fnFeatureHtmlLength( oSettings );
616 else if ( cOption == 'f' && oSettings.oFeatures.bFilter )
619 nTmp = _fnFeatureHtmlFilter( oSettings );
622 else if ( cOption == 'r' && oSettings.oFeatures.bProcessing )
625 nTmp = _fnFeatureHtmlProcessing( oSettings );
628 else if ( cOption == 't' )
631 nTmp = _fnFeatureHtmlTable( oSettings );
634 else if ( cOption == 'i' && oSettings.oFeatures.bInfo )
637 nTmp = _fnFeatureHtmlInfo( oSettings );
640 else if ( cOption == 'p' && oSettings.oFeatures.bPaginate )
643 nTmp = _fnFeatureHtmlPaginate( oSettings );
646 else if ( DataTable.ext.aoFeatures.length !== 0 )
648 /* Plug-in features */
649 var aoFeatures = DataTable.ext.aoFeatures;
650 for ( var k=0, kLen=aoFeatures.length ; k<kLen ; k++ )
652 if ( cOption == aoFeatures[k].cFeature )
654 nTmp = aoFeatures[k].fnInit( oSettings );
664 /* Add to the 2D features array */
665 if ( iPushFeature == 1 && nTmp !== null )
667 if ( typeof oSettings.aanFeatures[cOption] !== 'object' )
669 oSettings.aanFeatures[cOption] = [];
671 oSettings.aanFeatures[cOption].push( nTmp );
672 nInsertNode.appendChild( nTmp );
676 /* Built our DOM structure - replace the holding div with what we want */
677 nHolding.parentNode.replaceChild( oSettings.nTableWrapper, nHolding );
682 * Use the DOM source to create up an array of header cells. The idea here is to
683 * create a layout grid (array) of rows x columns, which contains a reference
684 * to the cell that that point in the grid (regardless of col/rowspan), such that
685 * any column / row could be removed and the new grid constructed
686 * @param array {object} aLayout Array to store the calculated layout in
687 * @param {node} nThead The header/footer element for the table
688 * @memberof DataTable#oApi
690 function _fnDetectHeader ( aLayout, nThead )
692 var nTrs = $(nThead).children('tr');
694 var i, k, l, iLen, jLen, iColShifted, iColumn, iColspan, iRowspan;
696 var fnShiftCol = function ( a, i, j ) {
704 aLayout.splice( 0, aLayout.length );
706 /* We know how many rows there are in the layout - so prep it */
707 for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
712 /* Calculate a layout array */
713 for ( i=0, iLen=nTrs.length ; i<iLen ; i++ )
718 /* For every cell in the row... */
719 nCell = nTr.firstChild;
721 if ( nCell.nodeName.toUpperCase() == "TD" ||
722 nCell.nodeName.toUpperCase() == "TH" )
724 /* Get the col and rowspan attributes from the DOM and sanitise them */
725 iColspan = nCell.getAttribute('colspan') * 1;
726 iRowspan = nCell.getAttribute('rowspan') * 1;
727 iColspan = (!iColspan || iColspan===0 || iColspan===1) ? 1 : iColspan;
728 iRowspan = (!iRowspan || iRowspan===0 || iRowspan===1) ? 1 : iRowspan;
730 /* There might be colspan cells already in this row, so shift our target
733 iColShifted = fnShiftCol( aLayout, i, iColumn );
735 /* Cache calculation for unique columns */
736 bUnique = iColspan === 1 ? true : false;
738 /* If there is col / rowspan, copy the information into the layout grid */
739 for ( l=0 ; l<iColspan ; l++ )
741 for ( k=0 ; k<iRowspan ; k++ )
743 aLayout[i+k][iColShifted+l] = {
747 aLayout[i+k].nTr = nTr;
751 nCell = nCell.nextSibling;
758 * Get an array of unique th elements, one for each column
759 * @param {object} oSettings dataTables settings object
760 * @param {node} nHeader automatically detect the layout from this node - optional
761 * @param {array} aLayout thead/tfoot layout from _fnDetectHeader - optional
762 * @returns array {node} aReturn list of unique th's
763 * @memberof DataTable#oApi
765 function _fnGetUniqueThs ( oSettings, nHeader, aLayout )
770 aLayout = oSettings.aoHeader;
774 _fnDetectHeader( aLayout, nHeader );
778 for ( var i=0, iLen=aLayout.length ; i<iLen ; i++ )
780 for ( var j=0, jLen=aLayout[i].length ; j<jLen ; j++ )
782 if ( aLayout[i][j].unique &&
783 (!aReturn[j] || !oSettings.bSortCellsTop) )
785 aReturn[j] = aLayout[i][j].cell;