From fa3d921eb3645d6277540c518c404d94c08c4340 Mon Sep 17 00:00:00 2001 From: Peter Cassell Date: Mon, 8 Jun 2020 19:52:22 +0100 Subject: [PATCH 1/3] Added functionality to allow cell data to be loaded from the last child of a td so that platforms that generate excess HTML such as Verj.io can successfully integrate with DataTables. This functionality is set using columnDefs -> srcDataFromLastChild: true/false - default false --- js/core/core.data.js | 6 +++++- js/model/model.defaults.columns.js | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/js/core/core.data.js b/js/core/core.data.js index b96969238..7f6dddc3c 100644 --- a/js/core/core.data.js +++ b/js/core/core.data.js @@ -618,7 +618,11 @@ function _fnGetRowElements( settings, row, colIdx, d ) var cellProcess = function ( cell ) { if ( colIdx === undefined || colIdx === i ) { col = columns[i]; - contents = $.trim(cell.innerHTML); + //if specified, read data from last child of cell + if(col.srcDataFromLastChild && cell.lastChild){ + cell = cell.lastChild; + } + contents = (cell.innerHTML)? cell.innerHTML.trim(): ''; if ( col && col._bAttrSrc ) { var setter = _fnSetObjectDataFn( col.mData._ ); diff --git a/js/model/model.defaults.columns.js b/js/model/model.defaults.columns.js index 0d10b4305..6e93bc0a1 100644 --- a/js/model/model.defaults.columns.js +++ b/js/model/model.defaults.columns.js @@ -703,6 +703,31 @@ DataTable.defaults.column = { */ "sName": "", + /** + * Defines whether the data is sourced directly from the td (including any HTML Structure) + * or whether the inner HTML structure is disregarded and that data is taken from the lastChild element. + * + * This is useful in environments where table data may be embedded in multiple levels of HTML after the element. + * @name DataTable.defaults.column.srcDataFromLastChild + * @type bool + * @default false + * + * @example + * // Using `columnDefs` + * $(document).ready( function() { + * $('#example').dataTable( { + * "columnDefs": [ + * { + * srcDataFromLastChild: true, + * targets : '_all' + * } + * ] + * } ); + * } ); + * + */ + "srcDataFromLastChild": false + /** * Defines a data source type for the ordering which can be used to read From 756c20de1dfe28df43ffab888e106027a40252d3 Mon Sep 17 00:00:00 2001 From: Peter Cassell Date: Mon, 8 Jun 2020 20:03:44 +0100 Subject: [PATCH 2/3] corrected syntax error --- js/model/model.defaults.columns.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/model/model.defaults.columns.js b/js/model/model.defaults.columns.js index 6e93bc0a1..ac67c3a57 100644 --- a/js/model/model.defaults.columns.js +++ b/js/model/model.defaults.columns.js @@ -726,7 +726,7 @@ DataTable.defaults.column = { * } ); * */ - "srcDataFromLastChild": false + "srcDataFromLastChild": false, /** From bec069729c9ffc6fb04cb9130a3810ed676e6e84 Mon Sep 17 00:00:00 2001 From: Peter Cassell Date: Tue, 9 Jun 2020 17:46:43 +0100 Subject: [PATCH 3/3] now use innerHTML for getting data rather than lastChild --- js/core/core.data.js | 10 ++++++---- js/model/model.defaults.columns.js | 16 ++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/js/core/core.data.js b/js/core/core.data.js index 7f6dddc3c..5db24b28f 100644 --- a/js/core/core.data.js +++ b/js/core/core.data.js @@ -618,11 +618,13 @@ function _fnGetRowElements( settings, row, colIdx, d ) var cellProcess = function ( cell ) { if ( colIdx === undefined || colIdx === i ) { col = columns[i]; - //if specified, read data from last child of cell - if(col.srcDataFromLastChild && cell.lastChild){ - cell = cell.lastChild; + //if specified, ignore additional markup and only store innerText as data + if(col.stripHTML){ + contents = (cell.innerText)? cell.innerText.trim(): ''; + } + else{ + contents = (cell.innerHTML)? cell.innerHTML.trim(): ''; } - contents = (cell.innerHTML)? cell.innerHTML.trim(): ''; if ( col && col._bAttrSrc ) { var setter = _fnSetObjectDataFn( col.mData._ ); diff --git a/js/model/model.defaults.columns.js b/js/model/model.defaults.columns.js index ac67c3a57..834ebc21f 100644 --- a/js/model/model.defaults.columns.js +++ b/js/model/model.defaults.columns.js @@ -703,12 +703,16 @@ DataTable.defaults.column = { */ "sName": "", + /** - * Defines whether the data is sourced directly from the td (including any HTML Structure) - * or whether the inner HTML structure is disregarded and that data is taken from the lastChild element. + * Defines whether the cell data includes the HTML content or not. + * This means that when the data contents of the cell are set, the innerText property is used rather than innerHTML. * - * This is useful in environments where table data may be embedded in multiple levels of HTML after the element. - * @name DataTable.defaults.column.srcDataFromLastChild + * This is useful in environments where table data may be embedded in multiple levels of HTML after the element for two reasons:- + * -DataTables extensions such as searchPanes can compare cell data without comparing HTML data. + * -Performance increase for processing tables which have a large amount of additional markup as less data is saved to memory. * + * + * @name DataTable.defaults.column.stripHTML * @type bool * @default false * @@ -718,7 +722,7 @@ DataTable.defaults.column = { * $('#example').dataTable( { * "columnDefs": [ * { - * srcDataFromLastChild: true, + * stripHTML: true, * targets : '_all' * } * ] @@ -726,7 +730,7 @@ DataTable.defaults.column = { * } ); * */ - "srcDataFromLastChild": false, + "stripHTML": false /**