2 * Perform a jQuery selector action on the table's TR elements (from the tbody) and
3 * return the resulting jQuery object.
4 * @param {string|node|jQuery} sSelector jQuery selector or node collection to act on
5 * @param {object} [oOpts] Optional parameters for modifying the rows to be included
6 * @param {string} [oOpts.filter=none] Select TR elements that meet the current filter
7 * criterion ("applied") or all TR elements (i.e. no filter).
8 * @param {string} [oOpts.order=current] Order of the TR elements in the processed array.
9 * Can be either 'current', whereby the current sorting of the table is used, or
10 * 'original' whereby the original order the data was read into the table is used.
11 * @param {string} [oOpts.page=all] Limit the selection to the currently displayed page
12 * ("current") or not ("all"). If 'current' is given, then order is assumed to be
13 * 'current' and filter is 'applied', regardless of what they might be given as.
14 * @returns {object} jQuery object, filtered by the given selector.
18 * $(document).ready(function() {
19 * var oTable = $('#example').dataTable();
21 * // Highlight every second row
22 * oTable.$('tr:odd').css('backgroundColor', 'blue');
26 * $(document).ready(function() {
27 * var oTable = $('#example').dataTable();
29 * // Filter to rows with 'Webkit' in them, add a background colour and then
30 * // remove the filter, thus highlighting the 'Webkit' rows only.
31 * oTable.fnFilter('Webkit');
32 * oTable.$('tr', {"filter": "applied"}).css('backgroundColor', 'blue');
33 * oTable.fnFilter('');
36 this.$ = function ( sSelector, oOpts )
38 var i, iLen, a = [], tr;
39 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
40 var aoData = oSettings.aoData;
41 var aiDisplay = oSettings.aiDisplay;
42 var aiDisplayMaster = oSettings.aiDisplayMaster;
49 oOpts = $.extend( {}, {
50 "filter": "none", // applied
51 "order": "current", // "original"
52 "page": "all" // current
55 // Current page implies that order=current and fitler=applied, since it is fairly
56 // senseless otherwise
57 if ( oOpts.page == 'current' )
59 for ( i=oSettings._iDisplayStart, iLen=oSettings.fnDisplayEnd() ; i<iLen ; i++ )
61 tr = aoData[ aiDisplay[i] ].nTr;
68 else if ( oOpts.order == "current" && oOpts.filter == "none" )
70 for ( i=0, iLen=aiDisplayMaster.length ; i<iLen ; i++ )
72 tr = aoData[ aiDisplayMaster[i] ].nTr;
79 else if ( oOpts.order == "current" && oOpts.filter == "applied" )
81 for ( i=0, iLen=aiDisplay.length ; i<iLen ; i++ )
83 tr = aoData[ aiDisplay[i] ].nTr;
90 else if ( oOpts.order == "original" && oOpts.filter == "none" )
92 for ( i=0, iLen=aoData.length ; i<iLen ; i++ )
94 tr = aoData[ i ].nTr ;
101 else if ( oOpts.order == "original" && oOpts.filter == "applied" )
103 for ( i=0, iLen=aoData.length ; i<iLen ; i++ )
105 tr = aoData[ i ].nTr;
106 if ( $.inArray( i, aiDisplay ) !== -1 && tr )
114 _fnLog( oSettings, 1, "Unknown selection options" );
117 /* We need to filter on the TR elements and also 'find' in their descendants
118 * to make the selector act like it would in a full table - so we need
119 * to build both results and then combine them together
122 var jqTRs = jqA.filter( sSelector );
123 var jqDescendants = jqA.find( sSelector );
125 return $( [].concat($.makeArray(jqTRs), $.makeArray(jqDescendants)) );
130 * Almost identical to $ in operation, but in this case returns the data for the matched
131 * rows - as such, the jQuery selector used should match TR row nodes or TD/TH cell nodes
132 * rather than any descendants, so the data can be obtained for the row/cell. If matching
133 * rows are found, the data returned is the original data array/object that was used to
134 * create the row (or a generated array if from a DOM source).
136 * This method is often useful in-combination with $ where both functions are given the
137 * same parameters and the array indexes will match identically.
138 * @param {string|node|jQuery} sSelector jQuery selector or node collection to act on
139 * @param {object} [oOpts] Optional parameters for modifying the rows to be included
140 * @param {string} [oOpts.filter=none] Select elements that meet the current filter
141 * criterion ("applied") or all elements (i.e. no filter).
142 * @param {string} [oOpts.order=current] Order of the data in the processed array.
143 * Can be either 'current', whereby the current sorting of the table is used, or
144 * 'original' whereby the original order the data was read into the table is used.
145 * @param {string} [oOpts.page=all] Limit the selection to the currently displayed page
146 * ("current") or not ("all"). If 'current' is given, then order is assumed to be
147 * 'current' and filter is 'applied', regardless of what they might be given as.
148 * @returns {array} Data for the matched elements. If any elements, as a result of the
149 * selector, were not TR, TD or TH elements in the DataTable, they will have a null
150 * entry in the array.
154 * $(document).ready(function() {
155 * var oTable = $('#example').dataTable();
157 * // Get the data from the first row in the table
158 * var data = oTable._('tr:first');
160 * // Do something useful with the data
161 * alert( "First cell is: "+data[0] );
165 * $(document).ready(function() {
166 * var oTable = $('#example').dataTable();
168 * // Filter to 'Webkit' and get all data for
169 * oTable.fnFilter('Webkit');
170 * var data = oTable._('tr', {"filter": "applied"});
172 * // Do something with the data
173 * alert( data.length+" rows matched the filter" );
176 this._ = function ( sSelector, oOpts )
180 var aTrs = this.$( sSelector, oOpts );
182 for ( i=0, iLen=aTrs.length ; i<iLen ; i++ )
184 aOut.push( this.fnGetData(aTrs[i]) );
192 * Add a single new row or multiple rows of data to the table. Please note
193 * that this is suitable for client-side processing only - if you are using
194 * server-side processing (i.e. "bServerSide": true), then to add data, you
195 * must add it to the data source, i.e. the server-side, through an Ajax call.
196 * @param {array|object} mData The data to be added to the table. This can be:
198 * <li>1D array of data - add a single row with the data provided</li>
199 * <li>2D array of arrays - add multiple rows in a single call</li>
200 * <li>object - data object when using <i>mData</i></li>
201 * <li>array of objects - multiple data objects when using <i>mData</i></li>
203 * @param {bool} [bRedraw=true] redraw the table or not
204 * @returns {array} An array of integers, representing the list of indexes in
205 * <i>aoData</i> ({@link DataTable.models.oSettings}) that have been added to
210 * // Global var for counter
213 * $(document).ready(function() {
214 * $('#example').dataTable();
217 * function fnClickAddRow() {
218 * $('#example').dataTable().fnAddData( [
228 this.fnAddData = function( mData, bRedraw )
230 if ( mData.length === 0 )
238 /* Find settings from table node */
239 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
241 /* Check if we want to add multiple rows or not */
242 if ( typeof mData[0] === "object" && mData[0] !== null )
244 for ( var i=0 ; i<mData.length ; i++ )
246 iTest = _fnAddData( oSettings, mData[i] );
251 aiReturn.push( iTest );
256 iTest = _fnAddData( oSettings, mData );
261 aiReturn.push( iTest );
264 oSettings.aiDisplay = oSettings.aiDisplayMaster.slice();
266 if ( bRedraw === undefined || bRedraw )
268 _fnReDraw( oSettings );
275 * This function will make DataTables recalculate the column sizes, based on the data
276 * contained in the table and the sizes applied to the columns (in the DOM, CSS or
277 * through the sWidth parameter). This can be useful when the width of the table's
278 * parent element changes (for example a window resize).
279 * @param {boolean} [bRedraw=true] Redraw the table or not, you will typically want to
283 * $(document).ready(function() {
284 * var oTable = $('#example').dataTable( {
285 * "sScrollY": "200px",
289 * $(window).bind('resize', function () {
290 * oTable.fnAdjustColumnSizing();
294 this.fnAdjustColumnSizing = function ( bRedraw )
296 var oSettings = _fnSettingsFromNode(this[DataTable.ext.iApiIndex]);
297 _fnAdjustColumnSizing( oSettings );
299 if ( bRedraw === undefined || bRedraw )
301 this.fnDraw( false );
303 else if ( oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "" )
305 /* If not redrawing, but scrolling, we want to apply the new column sizes anyway */
306 this.oApi._fnScrollDraw(oSettings);
312 * Quickly and simply clear a table
313 * @param {bool} [bRedraw=true] redraw the table or not
317 * $(document).ready(function() {
318 * var oTable = $('#example').dataTable();
320 * // Immediately 'nuke' the current rows (perhaps waiting for an Ajax callback...)
321 * oTable.fnClearTable();
324 this.fnClearTable = function( bRedraw )
326 /* Find settings from table node */
327 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
328 _fnClearTable( oSettings );
330 if ( bRedraw === undefined || bRedraw )
332 _fnDraw( oSettings );
338 * The exact opposite of 'opening' a row, this function will close any rows which
339 * are currently 'open'.
340 * @param {node} nTr the table row to 'close'
341 * @returns {int} 0 on success, or 1 if failed (can't find the row)
345 * $(document).ready(function() {
348 * // 'open' an information row when a row is clicked on
349 * $('#example tbody tr').click( function () {
350 * if ( oTable.fnIsOpen(this) ) {
351 * oTable.fnClose( this );
353 * oTable.fnOpen( this, "Temporary row opened", "info_row" );
357 * oTable = $('#example').dataTable();
360 this.fnClose = function( nTr )
362 /* Find settings from table node */
363 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
365 for ( var i=0 ; i<oSettings.aoOpenRows.length ; i++ )
367 if ( oSettings.aoOpenRows[i].nParent == nTr )
369 var nTrParent = oSettings.aoOpenRows[i].nTr.parentNode;
372 /* Remove it if it is currently on display */
373 nTrParent.removeChild( oSettings.aoOpenRows[i].nTr );
375 oSettings.aoOpenRows.splice( i, 1 );
384 * Remove a row for the table
385 * @param {mixed} mTarget The index of the row from aoData to be deleted, or
386 * the TR element you want to delete
387 * @param {function|null} [fnCallBack] Callback function
388 * @param {bool} [bRedraw=true] Redraw the table or not
389 * @returns {array} The row that was deleted
393 * $(document).ready(function() {
394 * var oTable = $('#example').dataTable();
396 * // Immediately remove the first row
397 * oTable.fnDeleteRow( 0 );
400 this.fnDeleteRow = function( mTarget, fnCallBack, bRedraw )
402 /* Find settings from table node */
403 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
404 var i, iLen, iAODataIndex;
406 iAODataIndex = (typeof mTarget === 'object') ?
407 _fnNodeToDataIndex(oSettings, mTarget) : mTarget;
409 /* Return the data array from this row */
410 var oData = oSettings.aoData.splice( iAODataIndex, 1 );
412 /* Update the _DT_RowIndex parameter */
413 for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
415 if ( oSettings.aoData[i].nTr !== null )
417 oSettings.aoData[i].nTr._DT_RowIndex = i;
421 /* Remove the target row from the search array */
422 var iDisplayIndex = $.inArray( iAODataIndex, oSettings.aiDisplay );
423 oSettings.asDataSearch.splice( iDisplayIndex, 1 );
425 /* Delete from the display arrays */
426 _fnDeleteIndex( oSettings.aiDisplayMaster, iAODataIndex );
427 _fnDeleteIndex( oSettings.aiDisplay, iAODataIndex );
429 /* If there is a user callback function - call it */
430 if ( typeof fnCallBack === "function" )
432 fnCallBack.call( this, oSettings, oData );
435 /* Check for an 'overflow' they case for displaying the table */
436 if ( oSettings._iDisplayStart >= oSettings.fnRecordsDisplay() )
438 oSettings._iDisplayStart -= oSettings._iDisplayLength;
439 if ( oSettings._iDisplayStart < 0 )
441 oSettings._iDisplayStart = 0;
445 if ( bRedraw === undefined || bRedraw )
447 _fnCalculateEnd( oSettings );
448 _fnDraw( oSettings );
456 * Restore the table to it's original state in the DOM by removing all of DataTables
457 * enhancements, alterations to the DOM structure of the table and event listeners.
458 * @param {boolean} [bRemove=false] Completely remove the table from the DOM
462 * $(document).ready(function() {
463 * // This example is fairly pointless in reality, but shows how fnDestroy can be used
464 * var oTable = $('#example').dataTable();
465 * oTable.fnDestroy();
468 this.fnDestroy = function ( bRemove )
470 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
471 var nOrig = oSettings.nTableWrapper.parentNode;
472 var nBody = oSettings.nTBody;
475 bRemove = (bRemove===undefined) ? false : bRemove;
477 /* Flag to note that the table is currently being destroyed - no action should be taken */
478 oSettings.bDestroying = true;
480 /* Fire off the destroy callbacks for plug-ins etc */
481 _fnCallbackFire( oSettings, "aoDestroyCallback", "destroy", [oSettings] );
483 /* If the table is not being removed, restore the hidden columns */
486 for ( i=0, iLen=oSettings.aoColumns.length ; i<iLen ; i++ )
488 if ( oSettings.aoColumns[i].bVisible === false )
490 this.fnSetColumnVis( i, true );
495 /* Blitz all DT events */
496 $(oSettings.nTableWrapper).find('*').andSelf().unbind('.DT');
498 /* If there is an 'empty' indicator row, remove it */
499 $('tbody>tr>td.'+oSettings.oClasses.sRowEmpty, oSettings.nTable).parent().remove();
501 /* When scrolling we had to break the table up - restore it */
502 if ( oSettings.nTable != oSettings.nTHead.parentNode )
504 $(oSettings.nTable).children('thead').remove();
505 oSettings.nTable.appendChild( oSettings.nTHead );
508 if ( oSettings.nTFoot && oSettings.nTable != oSettings.nTFoot.parentNode )
510 $(oSettings.nTable).children('tfoot').remove();
511 oSettings.nTable.appendChild( oSettings.nTFoot );
514 /* Remove the DataTables generated nodes, events and classes */
515 oSettings.nTable.parentNode.removeChild( oSettings.nTable );
516 $(oSettings.nTableWrapper).remove();
518 oSettings.aaSorting = [];
519 oSettings.aaSortingFixed = [];
520 _fnSortingClasses( oSettings );
522 $(_fnGetTrNodes( oSettings )).removeClass( oSettings.asStripeClasses.join(' ') );
524 $('th, td', oSettings.nTHead).removeClass( [
525 oSettings.oClasses.sSortable,
526 oSettings.oClasses.sSortableAsc,
527 oSettings.oClasses.sSortableDesc,
528 oSettings.oClasses.sSortableNone ].join(' ')
530 if ( oSettings.bJUI )
532 $('th span.'+oSettings.oClasses.sSortIcon
533 + ', td span.'+oSettings.oClasses.sSortIcon, oSettings.nTHead).remove();
535 $('th, td', oSettings.nTHead).each( function () {
536 var jqWrapper = $('div.'+oSettings.oClasses.sSortJUIWrapper, this);
537 var kids = jqWrapper.contents();
538 $(this).append( kids );
543 /* Add the TR elements back into the table in their original order */
544 if ( !bRemove && oSettings.nTableReinsertBefore )
546 nOrig.insertBefore( oSettings.nTable, oSettings.nTableReinsertBefore );
550 nOrig.appendChild( oSettings.nTable );
553 for ( i=0, iLen=oSettings.aoData.length ; i<iLen ; i++ )
555 if ( oSettings.aoData[i].nTr !== null )
557 nBody.appendChild( oSettings.aoData[i].nTr );
561 /* Restore the width of the original table */
562 if ( oSettings.oFeatures.bAutoWidth === true )
564 oSettings.nTable.style.width = _fnStringToCss(oSettings.sDestroyWidth);
567 /* If the were originally stripe classes - then we add them back here. Note
568 * this is not fool proof (for example if not all rows had stripe classes - but
569 * it's a good effort without getting carried away
571 iLen = oSettings.asDestroyStripes.length;
574 var anRows = $(nBody).children('tr');
575 for ( i=0 ; i<iLen ; i++ )
577 anRows.filter(':nth-child(' + iLen + 'n + ' + i + ')').addClass( oSettings.asDestroyStripes[i] );
581 /* Remove the settings object from the settings array */
582 for ( i=0, iLen=DataTable.settings.length ; i<iLen ; i++ )
584 if ( DataTable.settings[i] == oSettings )
586 DataTable.settings.splice( i, 1 );
598 * @param {bool} [bComplete=true] Re-filter and resort (if enabled) the table before the draw.
602 * $(document).ready(function() {
603 * var oTable = $('#example').dataTable();
605 * // Re-draw the table - you wouldn't want to do it here, but it's an example :-)
609 this.fnDraw = function( bComplete )
611 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
612 if ( bComplete === false )
614 _fnCalculateEnd( oSettings );
615 _fnDraw( oSettings );
619 _fnReDraw( oSettings );
625 * Filter the input based on data
626 * @param {string} sInput String to filter the table on
627 * @param {int|null} [iColumn] Column to limit filtering to
628 * @param {bool} [bRegex=false] Treat as regular expression or not
629 * @param {bool} [bSmart=true] Perform smart filtering or not
630 * @param {bool} [bShowGlobal=true] Show the input global filter in it's input box(es)
631 * @param {bool} [bCaseInsensitive=true] Do case-insensitive matching (true) or not (false)
635 * $(document).ready(function() {
636 * var oTable = $('#example').dataTable();
638 * // Sometime later - filter...
639 * oTable.fnFilter( 'test string' );
642 this.fnFilter = function( sInput, iColumn, bRegex, bSmart, bShowGlobal, bCaseInsensitive )
644 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
646 if ( !oSettings.oFeatures.bFilter )
651 if ( bRegex === undefined || bRegex === null )
656 if ( bSmart === undefined || bSmart === null )
661 if ( bShowGlobal === undefined || bShowGlobal === null )
666 if ( bCaseInsensitive === undefined || bCaseInsensitive === null )
668 bCaseInsensitive = true;
671 if ( iColumn === undefined || iColumn === null )
674 _fnFilterComplete( oSettings, {
678 "bCaseInsensitive": bCaseInsensitive
681 if ( bShowGlobal && oSettings.aanFeatures.f )
683 var n = oSettings.aanFeatures.f;
684 for ( var i=0, iLen=n.length ; i<iLen ; i++ )
686 // IE9 throws an 'unknown error' if document.activeElement is used
687 // inside an iframe or frame...
689 if ( n[i]._DT_Input != document.activeElement )
691 $(n[i]._DT_Input).val( sInput );
695 $(n[i]._DT_Input).val( sInput );
702 /* Single column filter */
703 $.extend( oSettings.aoPreSearchCols[ iColumn ], {
704 "sSearch": sInput+"",
707 "bCaseInsensitive": bCaseInsensitive
709 _fnFilterComplete( oSettings, oSettings.oPreviousSearch, 1 );
715 * Get the data for the whole table, an individual row or an individual cell based on the
716 * provided parameters.
717 * @param {int|node} [mRow] A TR row node, TD/TH cell node or an integer. If given as
718 * a TR node then the data source for the whole row will be returned. If given as a
719 * TD/TH cell node then iCol will be automatically calculated and the data for the
720 * cell returned. If given as an integer, then this is treated as the aoData internal
721 * data index for the row (see fnGetPosition) and the data for that row used.
722 * @param {int} [iCol] Optional column index that you want the data of.
723 * @returns {array|object|string} If mRow is undefined, then the data for all rows is
724 * returned. If mRow is defined, just data for that row, and is iCol is
725 * defined, only data for the designated cell is returned.
730 * $(document).ready(function() {
731 * oTable = $('#example').dataTable();
733 * oTable.$('tr').click( function () {
734 * var data = oTable.fnGetData( this );
735 * // ... do something with the array / object of data for the row
740 * // Individual cell data
741 * $(document).ready(function() {
742 * oTable = $('#example').dataTable();
744 * oTable.$('td').click( function () {
745 * var sData = oTable.fnGetData( this );
746 * alert( 'The cell clicked on had the value of '+sData );
750 this.fnGetData = function( mRow, iCol )
752 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
754 if ( mRow !== undefined )
757 if ( typeof mRow === 'object' )
759 var sNode = mRow.nodeName.toLowerCase();
762 iRow = _fnNodeToDataIndex(oSettings, mRow);
764 else if ( sNode === "td" )
766 iRow = _fnNodeToDataIndex(oSettings, mRow.parentNode);
767 iCol = _fnNodeToColumnIndex( oSettings, iRow, mRow );
771 if ( iCol !== undefined )
773 return _fnGetCellData( oSettings, iRow, iCol, '' );
775 return (oSettings.aoData[iRow]!==undefined) ?
776 oSettings.aoData[iRow]._aData : null;
778 return _fnGetDataMaster( oSettings );
783 * Get an array of the TR nodes that are used in the table's body. Note that you will
784 * typically want to use the '$' API method in preference to this as it is more
786 * @param {int} [iRow] Optional row index for the TR element you want
787 * @returns {array|node} If iRow is undefined, returns an array of all TR elements
788 * in the table's body, or iRow is defined, just the TR element requested.
792 * $(document).ready(function() {
793 * var oTable = $('#example').dataTable();
795 * // Get the nodes from the table
796 * var nNodes = oTable.fnGetNodes( );
799 this.fnGetNodes = function( iRow )
801 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
803 if ( iRow !== undefined ) {
804 return (oSettings.aoData[iRow]!==undefined) ?
805 oSettings.aoData[iRow].nTr : null;
807 return _fnGetTrNodes( oSettings );
812 * Get the array indexes of a particular cell from it's DOM element
813 * and column index including hidden columns
814 * @param {node} nNode this can either be a TR, TD or TH in the table's body
815 * @returns {int} If nNode is given as a TR, then a single index is returned, or
816 * if given as a cell, an array of [row index, column index (visible),
817 * column index (all)] is given.
821 * $(document).ready(function() {
822 * $('#example tbody td').click( function () {
823 * // Get the position of the current data from the node
824 * var aPos = oTable.fnGetPosition( this );
826 * // Get the data array for this row
827 * var aData = oTable.fnGetData( aPos[0] );
829 * // Update the data array and return the value
830 * aData[ aPos[1] ] = 'clicked';
831 * this.innerHTML = 'clicked';
835 * oTable = $('#example').dataTable();
838 this.fnGetPosition = function( nNode )
840 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
841 var sNodeName = nNode.nodeName.toUpperCase();
843 if ( sNodeName == "TR" )
845 return _fnNodeToDataIndex(oSettings, nNode);
847 else if ( sNodeName == "TD" || sNodeName == "TH" )
849 var iDataIndex = _fnNodeToDataIndex( oSettings, nNode.parentNode );
850 var iColumnIndex = _fnNodeToColumnIndex( oSettings, iDataIndex, nNode );
851 return [ iDataIndex, _fnColumnIndexToVisible(oSettings, iColumnIndex ), iColumnIndex ];
858 * Check to see if a row is 'open' or not.
859 * @param {node} nTr the table row to check
860 * @returns {boolean} true if the row is currently open, false otherwise
864 * $(document).ready(function() {
867 * // 'open' an information row when a row is clicked on
868 * $('#example tbody tr').click( function () {
869 * if ( oTable.fnIsOpen(this) ) {
870 * oTable.fnClose( this );
872 * oTable.fnOpen( this, "Temporary row opened", "info_row" );
876 * oTable = $('#example').dataTable();
879 this.fnIsOpen = function( nTr )
881 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
882 var aoOpenRows = oSettings.aoOpenRows;
884 for ( var i=0 ; i<oSettings.aoOpenRows.length ; i++ )
886 if ( oSettings.aoOpenRows[i].nParent == nTr )
896 * This function will place a new row directly after a row which is currently
897 * on display on the page, with the HTML contents that is passed into the
898 * function. This can be used, for example, to ask for confirmation that a
899 * particular record should be deleted.
900 * @param {node} nTr The table row to 'open'
901 * @param {string|node|jQuery} mHtml The HTML to put into the row
902 * @param {string} sClass Class to give the new TD cell
903 * @returns {node} The row opened. Note that if the table row passed in as the
904 * first parameter, is not found in the table, this method will silently
909 * $(document).ready(function() {
912 * // 'open' an information row when a row is clicked on
913 * $('#example tbody tr').click( function () {
914 * if ( oTable.fnIsOpen(this) ) {
915 * oTable.fnClose( this );
917 * oTable.fnOpen( this, "Temporary row opened", "info_row" );
921 * oTable = $('#example').dataTable();
924 this.fnOpen = function( nTr, mHtml, sClass )
926 /* Find settings from table node */
927 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
929 /* Check that the row given is in the table */
930 var nTableRows = _fnGetTrNodes( oSettings );
931 if ( $.inArray(nTr, nTableRows) === -1 )
936 /* the old open one if there is one */
939 var nNewRow = document.createElement("tr");
940 var nNewCell = document.createElement("td");
941 nNewRow.appendChild( nNewCell );
942 nNewCell.className = sClass;
943 nNewCell.colSpan = _fnVisbleColumns( oSettings );
945 if (typeof mHtml === "string")
947 nNewCell.innerHTML = mHtml;
951 $(nNewCell).html( mHtml );
954 /* If the nTr isn't on the page at the moment - then we don't insert at the moment */
955 var nTrs = $('tr', oSettings.nTBody);
956 if ( $.inArray(nTr, nTrs) != -1 )
958 $(nNewRow).insertAfter(nTr);
961 oSettings.aoOpenRows.push( {
971 * Change the pagination - provides the internal logic for pagination in a simple API
972 * function. With this function you can have a DataTables table go to the next,
973 * previous, first or last pages.
974 * @param {string|int} mAction Paging action to take: "first", "previous", "next" or "last"
975 * or page number to jump to (integer), note that page 0 is the first page.
976 * @param {bool} [bRedraw=true] Redraw the table or not
980 * $(document).ready(function() {
981 * var oTable = $('#example').dataTable();
982 * oTable.fnPageChange( 'next' );
985 this.fnPageChange = function ( mAction, bRedraw )
987 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
988 _fnPageChange( oSettings, mAction );
989 _fnCalculateEnd( oSettings );
991 if ( bRedraw === undefined || bRedraw )
993 _fnDraw( oSettings );
999 * Show a particular column
1000 * @param {int} iCol The column whose display should be changed
1001 * @param {bool} bShow Show (true) or hide (false) the column
1002 * @param {bool} [bRedraw=true] Redraw the table or not
1006 * $(document).ready(function() {
1007 * var oTable = $('#example').dataTable();
1009 * // Hide the second column after initialisation
1010 * oTable.fnSetColumnVis( 1, false );
1013 this.fnSetColumnVis = function ( iCol, bShow, bRedraw )
1015 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
1017 var aoColumns = oSettings.aoColumns;
1018 var aoData = oSettings.aoData;
1019 var nTd, bAppend, iBefore;
1021 /* No point in doing anything if we are requesting what is already true */
1022 if ( aoColumns[iCol].bVisible == bShow )
1027 /* Show the column */
1031 for ( i=0 ; i<iCol ; i++ )
1033 if ( aoColumns[i].bVisible )
1039 /* Need to decide if we should use appendChild or insertBefore */
1040 bAppend = (iInsert >= _fnVisbleColumns( oSettings ));
1042 /* Which coloumn should we be inserting before? */
1045 for ( i=iCol ; i<aoColumns.length ; i++ )
1047 if ( aoColumns[i].bVisible )
1055 for ( i=0, iLen=aoData.length ; i<iLen ; i++ )
1057 if ( aoData[i].nTr !== null )
1061 aoData[i].nTr.appendChild(
1062 aoData[i]._anHidden[iCol]
1067 aoData[i].nTr.insertBefore(
1068 aoData[i]._anHidden[iCol],
1069 _fnGetTdNodes( oSettings, i )[iBefore] );
1076 /* Remove a column from display */
1077 for ( i=0, iLen=aoData.length ; i<iLen ; i++ )
1079 if ( aoData[i].nTr !== null )
1081 nTd = _fnGetTdNodes( oSettings, i )[iCol];
1082 aoData[i]._anHidden[iCol] = nTd;
1083 nTd.parentNode.removeChild( nTd );
1088 /* Clear to set the visible flag */
1089 aoColumns[iCol].bVisible = bShow;
1091 /* Redraw the header and footer based on the new column visibility */
1092 _fnDrawHead( oSettings, oSettings.aoHeader );
1093 if ( oSettings.nTFoot )
1095 _fnDrawHead( oSettings, oSettings.aoFooter );
1098 /* If there are any 'open' rows, then we need to alter the colspan for this col change */
1099 for ( i=0, iLen=oSettings.aoOpenRows.length ; i<iLen ; i++ )
1101 oSettings.aoOpenRows[i].nTr.colSpan = _fnVisbleColumns( oSettings );
1104 /* Do a redraw incase anything depending on the table columns needs it
1105 * (built-in: scrolling)
1107 if ( bRedraw === undefined || bRedraw )
1109 _fnAdjustColumnSizing( oSettings );
1110 _fnDraw( oSettings );
1113 _fnSaveState( oSettings );
1118 * Get the settings for a particular table for external manipulation
1119 * @returns {object} DataTables settings object. See
1120 * {@link DataTable.models.oSettings}
1124 * $(document).ready(function() {
1125 * var oTable = $('#example').dataTable();
1126 * var oSettings = oTable.fnSettings();
1128 * // Show an example parameter from the settings
1129 * alert( oSettings._iDisplayStart );
1132 this.fnSettings = function()
1134 return _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
1139 * Sort the table by a particular column
1140 * @param {int} iCol the data index to sort on. Note that this will not match the
1141 * 'display index' if you have hidden data entries
1145 * $(document).ready(function() {
1146 * var oTable = $('#example').dataTable();
1148 * // Sort immediately with columns 0 and 1
1149 * oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );
1152 this.fnSort = function( aaSort )
1154 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
1155 oSettings.aaSorting = aaSort;
1156 _fnSort( oSettings );
1161 * Attach a sort listener to an element for a given column
1162 * @param {node} nNode the element to attach the sort listener to
1163 * @param {int} iColumn the column that a click on this node will sort on
1164 * @param {function} [fnCallback] callback function when sort is run
1168 * $(document).ready(function() {
1169 * var oTable = $('#example').dataTable();
1171 * // Sort on column 1, when 'sorter' is clicked on
1172 * oTable.fnSortListener( document.getElementById('sorter'), 1 );
1175 this.fnSortListener = function( nNode, iColumn, fnCallback )
1177 _fnSortAttachListener( _fnSettingsFromNode( this[DataTable.ext.iApiIndex] ), nNode, iColumn,
1183 * Update a table cell or row - this method will accept either a single value to
1184 * update the cell with, an array of values with one element for each column or
1185 * an object in the same format as the original data source. The function is
1186 * self-referencing in order to make the multi column updates easier.
1187 * @param {object|array|string} mData Data to update the cell/row with
1188 * @param {node|int} mRow TR element you want to update or the aoData index
1189 * @param {int} [iColumn] The column to update (not used of mData is an array or object)
1190 * @param {bool} [bRedraw=true] Redraw the table or not
1191 * @param {bool} [bAction=true] Perform pre-draw actions or not
1192 * @returns {int} 0 on success, 1 on error
1196 * $(document).ready(function() {
1197 * var oTable = $('#example').dataTable();
1198 * oTable.fnUpdate( 'Example update', 0, 0 ); // Single cell
1199 * oTable.fnUpdate( ['a', 'b', 'c', 'd', 'e'], 1, 0 ); // Row
1202 this.fnUpdate = function( mData, mRow, iColumn, bRedraw, bAction )
1204 var oSettings = _fnSettingsFromNode( this[DataTable.ext.iApiIndex] );
1205 var i, iLen, sDisplay;
1206 var iRow = (typeof mRow === 'object') ?
1207 _fnNodeToDataIndex(oSettings, mRow) : mRow;
1209 if ( $.isArray(mData) && iColumn === undefined )
1211 /* Array update - update the whole row */
1212 oSettings.aoData[iRow]._aData = mData.slice();
1214 /* Flag to the function that we are recursing */
1215 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
1217 this.fnUpdate( _fnGetCellData( oSettings, iRow, i ), iRow, i, false, false );
1220 else if ( $.isPlainObject(mData) && iColumn === undefined )
1222 /* Object update - update the whole row - assume the developer gets the object right */
1223 oSettings.aoData[iRow]._aData = $.extend( true, {}, mData );
1225 for ( i=0 ; i<oSettings.aoColumns.length ; i++ )
1227 this.fnUpdate( _fnGetCellData( oSettings, iRow, i ), iRow, i, false, false );
1232 /* Individual cell update */
1233 _fnSetCellData( oSettings, iRow, iColumn, mData );
1234 sDisplay = _fnGetCellData( oSettings, iRow, iColumn, 'display' );
1236 var oCol = oSettings.aoColumns[iColumn];
1237 if ( oCol.fnRender !== null )
1239 sDisplay = _fnRender( oSettings, iRow, iColumn );
1240 if ( oCol.bUseRendered )
1242 _fnSetCellData( oSettings, iRow, iColumn, sDisplay );
1246 if ( oSettings.aoData[iRow].nTr !== null )
1248 /* Do the actual HTML update */
1249 _fnGetTdNodes( oSettings, iRow )[iColumn].innerHTML = sDisplay;
1253 /* Modify the search index for this row (strictly this is likely not needed, since fnReDraw
1254 * will rebuild the search array - however, the redraw might be disabled by the user)
1256 var iDisplayIndex = $.inArray( iRow, oSettings.aiDisplay );
1257 oSettings.asDataSearch[iDisplayIndex] = _fnBuildSearchRow(
1259 _fnGetRowData( oSettings, iRow, 'filter', _fnGetColumns( oSettings, 'bSearchable' ) )
1262 /* Perform pre-draw actions */
1263 if ( bAction === undefined || bAction )
1265 _fnAdjustColumnSizing( oSettings );
1268 /* Redraw the table */
1269 if ( bRedraw === undefined || bRedraw )
1271 _fnReDraw( oSettings );
1278 * Provide a common method for plug-ins to check the version of DataTables being used, in order
1279 * to ensure compatibility.
1280 * @param {string} sVersion Version string to check for, in the format "X.Y.Z". Note that the
1281 * formats "X" and "X.Y" are also acceptable.
1282 * @returns {boolean} true if this version of DataTables is greater or equal to the required
1283 * version, or false if this version of DataTales is not suitable
1288 * $(document).ready(function() {
1289 * var oTable = $('#example').dataTable();
1290 * alert( oTable.fnVersionCheck( '1.9.0' ) );
1293 this.fnVersionCheck = DataTable.ext.fnVersionCheck;