From e2c11c924ea36b5d066dda60f87bf40a72430cc2 Mon Sep 17 00:00:00 2001 From: Michael Lux Date: Sun, 19 Oct 2014 14:27:08 +0200 Subject: [PATCH] advanced rendering --- js/core/core.ajax.js | 2 +- js/core/core.columns.js | 6 ++-- js/core/core.constructor.js | 1 + js/core/core.data.js | 61 ++++++++++++++++++++++++++----------- js/core/core.draw.js | 2 +- js/core/core.sizing.js | 13 +++++--- 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/js/core/core.ajax.js b/js/core/core.ajax.js index 302fd44fd..f35cdbb16 100644 --- a/js/core/core.ajax.js +++ b/js/core/core.ajax.js @@ -339,7 +339,7 @@ function _fnAjaxDataSrc ( oSettings, json ) } return dataSrc !== "" ? - _fnGetObjectDataFn( dataSrc )( json ) : + _fnGetObjectDataFn( dataSrc, oSettings )( json ) : json; } diff --git a/js/core/core.columns.js b/js/core/core.columns.js index ec9483c38..2df7bd5f9 100644 --- a/js/core/core.columns.js +++ b/js/core/core.columns.js @@ -97,8 +97,8 @@ function _fnColumnOptions( oSettings, iCol, oOptions ) /* Cache the data get and set functions for speed */ var mDataSrc = oCol.mData; - var mData = _fnGetObjectDataFn( mDataSrc ); - var mRender = oCol.mRender ? _fnGetObjectDataFn( oCol.mRender ) : null; + var mData = _fnGetObjectDataFn( mDataSrc, oSettings ); + var mRender = oCol.mRender ? _fnGetObjectDataFn( oCol.mRender, oSettings ) : null; var attrTest = function( src ) { return typeof src === 'string' && src.indexOf('@') !== -1; @@ -111,7 +111,7 @@ function _fnColumnOptions( oSettings, iCol, oOptions ) var innerData = mData( rowData, type, undefined, meta ); return mRender && type ? - mRender( innerData, type, rowData, meta ) : + mRender.call( this, innerData, type, rowData, meta ) : innerData; }; oCol.fnSetData = function ( rowData, val, meta ) { diff --git a/js/core/core.constructor.js b/js/core/core.constructor.js index 02d1181ad..6f9d21496 100644 --- a/js/core/core.constructor.js +++ b/js/core/core.constructor.js @@ -136,6 +136,7 @@ _fnMap( oSettings, oInit, [ "fnStateLoadCallback", "fnStateSaveCallback", "renderer", + "newCellRender", [ "iCookieDuration", "iStateDuration" ], // backwards compat [ "oSearch", "oPreviousSearch" ], [ "aoSearchCols", "aoPreSearchCols" ], diff --git a/js/core/core.data.js b/js/core/core.data.js index 829f842ab..dd7b9dbe4 100644 --- a/js/core/core.data.js +++ b/js/core/core.data.js @@ -117,11 +117,23 @@ function _fnGetCellData( settings, rowIdx, colIdx, type ) var col = settings.aoColumns[colIdx]; var rowData = settings.aoData[rowIdx]._aData; var defaultContent = col.sDefaultContent; - var cellData = col.fnGetData( rowData, type, { + var meta = { settings: settings, - row: rowIdx, - col: colIdx - } ); + row: rowIdx, + col: colIdx + }; + var cellData = col.fnGetData.call(this, rowData, type, meta); + + //compatability stuff + if( type === 'display' ) { + if(cellData === undefined) { + //default behaviour with newCellRender, nothting to do here... + return; + } else { + //compatability: set node content to return value, if there is one + this.innerHTML = cellData; + } + } if ( cellData === undefined ) { if ( settings.iDrawError != draw && defaultContent === null ) { @@ -143,7 +155,7 @@ function _fnGetCellData( settings, rowIdx, colIdx, type ) return cellData.call( rowData ); } - if ( cellData === null && type == 'display' ) { + if ( cellData === null && ( type === 'display' || type === 'adjust' ) ) { return ''; } return cellData; @@ -192,10 +204,11 @@ function _fnSplitObjNotation( str ) * Return a function that can be used to get data from a source object, taking * into account the ability to use nested objects as a source * @param {string|int|function} mSource The data source for the object + * @param {object} oSettings dataTables settings object * @returns {function} Data get function * @memberof DataTable#oApi */ -function _fnGetObjectDataFn( mSource ) +function _fnGetObjectDataFn( mSource, oSettings ) { if ( $.isPlainObject( mSource ) ) { @@ -203,14 +216,21 @@ function _fnGetObjectDataFn( mSource ) var o = {}; $.each( mSource, function (key, val) { if ( val ) { - o[key] = _fnGetObjectDataFn( val ); + o[key] = _fnGetObjectDataFn( val, oSettings ); } } ); + //backwards compatibility + if(oSettings === undefined && oSettings.newCellRender !== true) { + if(o.adjust === undefined && o.display !== undefined) { + o.adjust = o.display; + } + } + return function (data, type, row, meta) { var t = o[type] || o._; return t !== undefined ? - t(data, type, row, meta) : + t.call(this, data, type, row, meta) : data; }; } @@ -223,9 +243,16 @@ function _fnGetObjectDataFn( mSource ) } else if ( typeof mSource === 'function' ) { - return function (data, type, row, meta) { - return mSource( data, type, row, meta ); - }; + if(oSettings !== undefined && oSettings.newCellRender === true) { + return function (data, type, row, meta) { + return mSource.call( this, data, type, row, meta ); + }; + } else { + return function (data, type, row, meta) { + //backwards-compatability + return mSource.call( this, data, type === 'adjust' ? 'display' : type, row, meta ); + }; + } } else if ( typeof mSource === 'string' && (mSource.indexOf('.') !== -1 || mSource.indexOf('[') !== -1 || mSource.indexOf('(') !== -1) ) @@ -236,7 +263,7 @@ function _fnGetObjectDataFn( mSource ) * return. This allows entire objects to be missing and sDefaultContent to * be used if defined, rather than throwing an error */ - var fetchData = function (data, type, src) { + var fetchData = function (data, src) { var arrayNotation, funcNotation, out, innerSrc; if ( src !== "" ) @@ -266,7 +293,7 @@ function _fnGetObjectDataFn( mSource ) // Traverse each entry in the array getting the properties requested for ( var j=0, jLen=data.length ; j').html( _fnGetCellData( settings, idx, colIdx, 'display' ) )[0] : - data.anCells[ colIdx ]; + if(data.nTr) { + return data.anCells[ colIdx ]; + } else { + // Might not have been created when deferred rendering + var tempTd = document.createElement("td"); + _fnGetCellData.call( tempTd, settings, idx, colIdx, 'display' ); + return tempTd; + } } @@ -290,7 +295,7 @@ function _fnGetMaxLenString( settings, colIdx ) var s, max=-1, maxIdx = -1; for ( var i=0, ien=settings.aoData.length ; i max ) {