From 5209e2f05884662e23e3c36bd92de455a9b2e149 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Wed, 31 Oct 2012 14:56:34 +0000 Subject: [PATCH] Dev: Removed the _fnGatherData method, replacing it with _fnAddTr and slightly updated _fnAddData / _fnCreateTr. This means that the new API add add existing TR element if needed, but more importantly it reduces code duplication and makes the code size smaller (almost 1K - 967 bytes exactly, minified). Must confess I was really hoping it would be more, but every little helps... --- media/js/jquery.dataTables.js | 254 +++++++++-------------------- media/src/api/api.internal.js | 2 +- media/src/core/core.constructor.js | 9 +- media/src/core/core.data.js | 181 ++++---------------- media/src/core/core.draw.js | 62 ++++--- 5 files changed, 152 insertions(+), 356 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 649d35e4..e282ab1a 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -533,16 +533,21 @@ * DOM source. * @param {object} oSettings dataTables settings object * @param {array} aData data array to be added + * @param {node} [nTr] TR element to add to the table - optional. If not given, + * DataTables will create a row automatically + * @param {array} [anTds] Array of TD|TH elements for the row - must be given + * if nTr is. * @returns {int} >=0 if successful (index of new aoData entry), -1 if failed * @memberof DataTable#oApi */ - function _fnAddData ( oSettings, aDataIn ) + function _fnAddData ( oSettings, aDataIn, nTr, anTds ) { var oCol; /* Create the object for storing information about this new row */ var iRow = oSettings.aoData.length; var oData = $.extend( true, {}, DataTable.models.oRow ); + oData._aData = aDataIn; oSettings.aoData.push( oData ); @@ -581,7 +586,7 @@ /* Create the DOM information */ if ( !oSettings.oFeatures.bDeferRender ) { - _fnCreateTr( oSettings, iRow ); + _fnCreateTr( oSettings, iRow, nTr, anTds ); } return iRow; @@ -589,167 +594,41 @@ /** - * Read in the data from the target table from the DOM + * Add one or more TR elements to the table. Generally we'd expect to + * use this for reading data from a DOM sourced table, but it could be + * used for an TR element. Note that if a TR is given, it is used (i.e. + * it is not cloned). * @param {object} oSettings dataTables settings object + * @param {array|node|jQuery} trs The TR element(s) to add to the table * @memberof DataTable#oApi */ - function _fnGatherData( oSettings ) + function _fnAddTr( oSettings, trs ) { - var iLoop, i, iLen, j, jLen, jInner, - nTds, nTrs, nTd, nTr, aLocalData, iThisIndex, - iRow, iRows, iColumn, iColumns, sNodeName, - oCol, oData; - - /* - * Process by row first - * Add the data object for the whole table - storing the tr node. Note - no point in getting - * DOM based data if we are going to go and replace it with Ajax source data. - */ - if ( oSettings.bDeferLoading || oSettings.sAjaxSource === null ) - { - nTr = oSettings.nTBody.firstChild; - while ( nTr ) - { - if ( nTr.nodeName.toUpperCase() == "TR" ) - { - iThisIndex = oSettings.aoData.length; - nTr._DT_RowIndex = iThisIndex; - oSettings.aoData.push( $.extend( true, {}, DataTable.models.oRow, { - "nTr": nTr - } ) ); - - oSettings.aiDisplayMaster.push( iThisIndex ); - nTd = nTr.firstChild; - jInner = 0; - while ( nTd ) - { - sNodeName = nTd.nodeName.toUpperCase(); - if ( sNodeName == "TD" || sNodeName == "TH" ) - { - _fnSetCellData( oSettings, iThisIndex, jInner, $.trim(nTd.innerHTML) ); - jInner++; - } - nTd = nTd.nextSibling; - } - } - nTr = nTr.nextSibling; - } + // Allow an individual node to be passed in + if ( ! trs instanceof $ ) { + trs = $(trs); } - - /* Gather in the TD elements of the Table - note that this is basically the same as - * fnGetTdNodes, but that function takes account of hidden columns, which we haven't yet - * setup! - */ - nTrs = _fnGetTrNodes( oSettings ); - nTds = []; - for ( i=0, iLen=nTrs.length ; i=0 if successful (index of new aoData entry), -1 if failed * @memberof DataTable#oApi */ -function _fnAddData ( oSettings, aDataIn ) +function _fnAddData ( oSettings, aDataIn, nTr, anTds ) { var oCol; /* Create the object for storing information about this new row */ var iRow = oSettings.aoData.length; var oData = $.extend( true, {}, DataTable.models.oRow ); + oData._aData = aDataIn; oSettings.aoData.push( oData ); @@ -52,7 +57,7 @@ function _fnAddData ( oSettings, aDataIn ) /* Create the DOM information */ if ( !oSettings.oFeatures.bDeferRender ) { - _fnCreateTr( oSettings, iRow ); + _fnCreateTr( oSettings, iRow, nTr, anTds ); } return iRow; @@ -60,167 +65,41 @@ function _fnAddData ( oSettings, aDataIn ) /** - * Read in the data from the target table from the DOM + * Add one or more TR elements to the table. Generally we'd expect to + * use this for reading data from a DOM sourced table, but it could be + * used for an TR element. Note that if a TR is given, it is used (i.e. + * it is not cloned). * @param {object} oSettings dataTables settings object + * @param {array|node|jQuery} trs The TR element(s) to add to the table * @memberof DataTable#oApi */ -function _fnGatherData( oSettings ) +function _fnAddTr( oSettings, trs ) { - var iLoop, i, iLen, j, jLen, jInner, - nTds, nTrs, nTd, nTr, aLocalData, iThisIndex, - iRow, iRows, iColumn, iColumns, sNodeName, - oCol, oData; - - /* - * Process by row first - * Add the data object for the whole table - storing the tr node. Note - no point in getting - * DOM based data if we are going to go and replace it with Ajax source data. - */ - if ( oSettings.bDeferLoading || oSettings.sAjaxSource === null ) - { - nTr = oSettings.nTBody.firstChild; - while ( nTr ) - { - if ( nTr.nodeName.toUpperCase() == "TR" ) - { - iThisIndex = oSettings.aoData.length; - nTr._DT_RowIndex = iThisIndex; - oSettings.aoData.push( $.extend( true, {}, DataTable.models.oRow, { - "nTr": nTr - } ) ); - - oSettings.aiDisplayMaster.push( iThisIndex ); - nTd = nTr.firstChild; - jInner = 0; - while ( nTd ) - { - sNodeName = nTd.nodeName.toUpperCase(); - if ( sNodeName == "TD" || sNodeName == "TH" ) - { - _fnSetCellData( oSettings, iThisIndex, jInner, $.trim(nTd.innerHTML) ); - jInner++; - } - nTd = nTd.nextSibling; - } - } - nTr = nTr.nextSibling; - } + // Allow an individual node to be passed in + if ( ! trs instanceof $ ) { + trs = $(trs); } - - /* Gather in the TD elements of the Table - note that this is basically the same as - * fnGetTdNodes, but that function takes account of hidden columns, which we haven't yet - * setup! - */ - nTrs = _fnGetTrNodes( oSettings ); - nTds = []; - for ( i=0, iLen=nTrs.length ; i