From bccc85ef9fbd0bf92b0af0abba8aec01eaa8d919 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Tue, 6 Dec 2011 16:05:53 +0000 Subject: [PATCH] New: For plug-in authors, the internal function _fnApplyColumnDefs has been created and made available to make it much easier to present aoColumns and aoColumnDefs options for plug-ins (the names of the parameters can be whatever you want). Basically the function will take each of the two arrays (or null if not available) and figure out what definations should be applied to what columns - and then callback to a function that will apply the configuration (specific to the plug-in). This is now also used for the DataTables aoColumns and aoColumnDefs. --- media/js/jquery.dataTables.js | 147 +++++++++++++++++------------ media/src/DataTables.js | 2 +- media/src/api/api.internal.js | 3 +- media/src/core/core.columns.js | 73 ++++++++++++++ media/src/core/core.constructor.js | 62 +----------- media/src/core/core.support.js | 2 - 6 files changed, 166 insertions(+), 123 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 54493cca..f13d79c1 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -25,7 +25,7 @@ * When considering jsLint, we need to allow eval() as it it is used for reading cookies */ /*jslint evil: true, undef: true, browser: true */ -/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnArrayCmp,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn*/ +/*globals $, jQuery,_fnExternApiFunc,_fnInitialise,_fnInitComplete,_fnLanguageCompat,_fnAddColumn,_fnColumnOptions,_fnAddData,_fnCreateTr,_fnGatherData,_fnBuildHead,_fnDrawHead,_fnDraw,_fnReDraw,_fnAjaxUpdate,_fnAjaxParameters,_fnAjaxUpdateDraw,_fnServerParams,_fnAddOptionsHtml,_fnFeatureHtmlTable,_fnScrollDraw,_fnAdjustColumnSizing,_fnFeatureHtmlFilter,_fnFilterComplete,_fnFilterCustom,_fnFilterColumn,_fnFilter,_fnBuildSearchArray,_fnBuildSearchRow,_fnFilterCreateSearch,_fnDataToSearch,_fnSort,_fnSortAttachListener,_fnSortingClasses,_fnFeatureHtmlPaginate,_fnPageChange,_fnFeatureHtmlInfo,_fnUpdateInfo,_fnFeatureHtmlLength,_fnFeatureHtmlProcessing,_fnProcessingDisplay,_fnVisibleToColumnIndex,_fnColumnIndexToVisible,_fnNodeToDataIndex,_fnVisbleColumns,_fnCalculateEnd,_fnConvertToWidth,_fnCalculateColumnWidths,_fnScrollingWidthAdjust,_fnGetWidestNode,_fnGetMaxLenString,_fnStringToCss,_fnArrayCmp,_fnDetectType,_fnSettingsFromNode,_fnGetDataMaster,_fnGetTrNodes,_fnGetTdNodes,_fnEscapeRegex,_fnDeleteIndex,_fnReOrderIndex,_fnColumnOrdering,_fnLog,_fnClearTable,_fnSaveState,_fnLoadState,_fnCreateCookie,_fnReadCookie,_fnDetectHeader,_fnGetUniqueThs,_fnScrollBarWidth,_fnApplyToChildren,_fnMap,_fnGetRowData,_fnGetCellData,_fnSetCellData,_fnGetObjectDataFn,_fnSetObjectDataFn,_fnApplyColumnDefs*/ (function($, window, document) { @@ -377,6 +377,79 @@ } + /** + * Take the column definitions and static columns arrays and calculate how + * they relate to column indexes. The callback function will then apply the + * definition found for a column to a suitable configuration object. + * @param {object} oSettings dataTables settings object + * @param {array} aoColDefs The aoColumnDefs array that is to be applied + * @param {array} aoCols The aoColumns array that defines columns individually + * @param {function} fn Callback function - takes two parameters, the calculated + * column index and the definition for that column. + * @private + */ + function _fnApplyColumnDefs( oSettings, aoColDefs, aoCols, fn ) + { + var i, iLen; + + // Column definitions with aTargets + if ( aoColDefs ) + { + /* Loop over the definitions array - loop in reverse so first instance has priority */ + for ( i=aoColDefs.length-1 ; i>=0 ; i-- ) + { + /* Each definition can target multiple columns, as it is an array */ + var aTargets = aoColDefs[i].aTargets; + if ( !$.isArray( aTargets ) ) + { + _fnLog( oSettings, 1, 'aTargets must be an array of targets, not a '+(typeof aTargets) ); + } + + for ( j=0, jLen=aTargets.length ; j= 0 ) + { + /* Add columns that we don't yet know about */ + while( oSettings.aoColumns.length <= aTargets[j] ) + { + _fnAddColumn( oSettings ); + } + + /* Integer, basic index */ + fn( aTargets[j], aoColDefs[i] ); + } + else if ( typeof aTargets[j] == 'number' && aTargets[j] < 0 ) + { + /* Negative integer, right to left column counting */ + fn( oSettings.aoColumns.length+aTargets[j], aoColDefs[i] ); + } + else if ( typeof aTargets[j] == 'string' ) + { + /* Class name matching on TH element */ + for ( k=0, kLen=oSettings.aoColumns.length ; k=0 ; i-- ) - { - /* Each column def can target multiple columns, as it is an array */ - var aTargets = oInit.aoColumnDefs[i].aTargets; - if ( !$.isArray( aTargets ) ) - { - _fnLog( oSettings, 1, 'aTargets must be an array of targets, not a '+(typeof aTargets) ); - } - for ( j=0, jLen=aTargets.length ; j= 0 ) - { - /* 0+ integer, left to right column counting. We add columns which are unknown - * automatically. Is this the right behaviour for this? We should at least - * log it in future. We cannot do this for the negative or class targets, only here. - */ - while( oSettings.aoColumns.length <= aTargets[j] ) - { - _fnAddColumn( oSettings ); - } - _fnColumnOptions( oSettings, aTargets[j], oInit.aoColumnDefs[i] ); - } - else if ( typeof aTargets[j] == 'number' && aTargets[j] < 0 ) - { - /* Negative integer, right to left column counting */ - _fnColumnOptions( oSettings, oSettings.aoColumns.length+aTargets[j], - oInit.aoColumnDefs[i] ); - } - else if ( typeof aTargets[j] == 'string' ) - { - /* Class name matching on TH element */ - for ( k=0, kLen=oSettings.aoColumns.length ; k=0 ; i-- ) + { + /* Each definition can target multiple columns, as it is an array */ + var aTargets = aoColDefs[i].aTargets; + if ( !$.isArray( aTargets ) ) + { + _fnLog( oSettings, 1, 'aTargets must be an array of targets, not a '+(typeof aTargets) ); + } + + for ( j=0, jLen=aTargets.length ; j= 0 ) + { + /* Add columns that we don't yet know about */ + while( oSettings.aoColumns.length <= aTargets[j] ) + { + _fnAddColumn( oSettings ); + } + + /* Integer, basic index */ + fn( aTargets[j], aoColDefs[i] ); + } + else if ( typeof aTargets[j] == 'number' && aTargets[j] < 0 ) + { + /* Negative integer, right to left column counting */ + fn( oSettings.aoColumns.length+aTargets[j], aoColDefs[i] ); + } + else if ( typeof aTargets[j] == 'string' ) + { + /* Class name matching on TH element */ + for ( k=0, kLen=oSettings.aoColumns.length ; k=0 ; i-- ) - { - /* Each column def can target multiple columns, as it is an array */ - var aTargets = oInit.aoColumnDefs[i].aTargets; - if ( !$.isArray( aTargets ) ) - { - _fnLog( oSettings, 1, 'aTargets must be an array of targets, not a '+(typeof aTargets) ); - } - for ( j=0, jLen=aTargets.length ; j= 0 ) - { - /* 0+ integer, left to right column counting. We add columns which are unknown - * automatically. Is this the right behaviour for this? We should at least - * log it in future. We cannot do this for the negative or class targets, only here. - */ - while( oSettings.aoColumns.length <= aTargets[j] ) - { - _fnAddColumn( oSettings ); - } - _fnColumnOptions( oSettings, aTargets[j], oInit.aoColumnDefs[i] ); - } - else if ( typeof aTargets[j] == 'number' && aTargets[j] < 0 ) - { - /* Negative integer, right to left column counting */ - _fnColumnOptions( oSettings, oSettings.aoColumns.length+aTargets[j], - oInit.aoColumnDefs[i] ); - } - else if ( typeof aTargets[j] == 'string' ) - { - /* Class name matching on TH element */ - for ( k=0, kLen=oSettings.aoColumns.length ; k