From 9065269ce041d164440b322c6784a48e9a1e8815 Mon Sep 17 00:00:00 2001 From: Wu Xiang Date: Tue, 21 Jul 2015 11:15:59 +0800 Subject: [PATCH] fix header rendering when there're multiple header rows --- js/dataTables.tableTools.js | 78 ++++++++++++++++++++++++++++++------- src/TableTools.js | 73 +++++++++++++++++++++++++++++----- 2 files changed, 127 insertions(+), 24 deletions(-) diff --git a/js/dataTables.tableTools.js b/js/dataTables.tableTools.js index 5a36416..e378cc7 100755 --- a/js/dataTables.tableTools.js +++ b/js/dataTables.tableTools.js @@ -1164,10 +1164,7 @@ TableTools.prototype = { .off( 'mouseup.DTTT_Select', 'tr' ); $.each( ZeroClipboard_TableTools.clients, function ( id, client ) { - if ( client.domElement !== undefined && - $(client.domElement).parents( that.dom.container ).length ) - { - console.log( 'delete', id ); + if ( $(client.domElement).parents( that.dom.container ).length ) { delete ZeroClipboard_TableTools.clients[ id ]; } } ); @@ -2157,6 +2154,62 @@ TableTools.prototype = { } }, + "_fnGetHeaders": function() { + var dt = this.s.dt; + var thRows = dt.nTHead.rows; + var numRows = thRows.length; + var matrix = []; + + // Iterate over each row of the header and add information to matrix. + for ( var rowIdx = 0; rowIdx < numRows; rowIdx++ ) { + var $row = $(thRows[rowIdx]); + + // Iterate over actual columns specified in this row. + var $ths = $row.children("th"); + for ( var colIdx = 0; colIdx < $ths.length; colIdx++ ) + { + var $th = $($ths.get(colIdx)); + var colspan = $th.attr("colspan") || 1; + var rowspan = $th.attr("rowspan") || 1; + var colCount = 0; + + // ----- add this cell's title to the matrix + if (matrix[rowIdx] === undefined) { + matrix[rowIdx] = []; // create array for this row + } + // find 1st empty cell + for ( var j = 0; j < (matrix[rowIdx]).length; j++, colCount++ ) { + if ( matrix[rowIdx][j] === "PLACEHOLDER" ) { + break; + } + } + var myColCount = colCount; + matrix[rowIdx][colCount++] = $th.text(); + + // ----- If title cell has colspan, add empty titles for extra cell width. + for ( var j = 1; j < colspan; j++ ) { + matrix[rowIdx][colCount++] = ""; + } + + // ----- If title cell has rowspan, add empty titles for extra cell height. + for ( var i = 1; i < rowspan; i++ ) { + var thisRow = rowIdx+i; + if ( matrix[thisRow] === undefined ) { + matrix[thisRow] = []; + } + // First add placeholder text for any previous columns. + for ( var j = (matrix[thisRow]).length; j < myColCount; j++ ) { + matrix[thisRow][j] = "PLACEHOLDER"; + } + for ( var j = 0; j < colspan; j++ ) { // and empty for my columns + matrix[thisRow][myColCount+j] = ""; + } + } + } + } + + return matrix; + }, /** * Get data from DataTables' internals and format it for output @@ -2186,21 +2239,18 @@ TableTools.prototype = { */ if ( oConfig.bHeader ) { - aRow = []; - - for ( i=0, iLen=dt.aoColumns.length ; i/g, "" ).replace(/^\s+|\s+$/g,""); + var headerMatrix = this._fnGetHeaders(); + for ( var rowIdx = 0; rowIdx < headerMatrix.length; rowIdx++ ) { + aRow = []; + for ( var colIdx = 0; colIdx < (headerMatrix[rowIdx]).length; colIdx++ ) { + sLoopData = headerMatrix[rowIdx][colIdx].replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,""); sLoopData = this._fnHtmlDecode( sLoopData ); aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) ); } + aData.push( aRow.join(oConfig.sFieldSeperator) ); } - - aData.push( aRow.join(oConfig.sFieldSeperator) ); - } + } bSelectedOnly = true; diff --git a/src/TableTools.js b/src/TableTools.js index cfd2a7c..6b0c326 100755 --- a/src/TableTools.js +++ b/src/TableTools.js @@ -1719,6 +1719,62 @@ TableTools.prototype = { } }, + "_fnGetHeaders": function() { + var dt = this.s.dt; + var thRows = dt.nTHead.rows; + var numRows = thRows.length; + var matrix = []; + + // Iterate over each row of the header and add information to matrix. + for ( var rowIdx = 0; rowIdx < numRows; rowIdx++ ) { + var $row = $(thRows[rowIdx]); + + // Iterate over actual columns specified in this row. + var $ths = $row.children("th"); + for ( var colIdx = 0; colIdx < $ths.length; colIdx++ ) + { + var $th = $($ths.get(colIdx)); + var colspan = $th.attr("colspan") || 1; + var rowspan = $th.attr("rowspan") || 1; + var colCount = 0; + + // ----- add this cell's title to the matrix + if (matrix[rowIdx] === undefined) { + matrix[rowIdx] = []; // create array for this row + } + // find 1st empty cell + for ( var j = 0; j < (matrix[rowIdx]).length; j++, colCount++ ) { + if ( matrix[rowIdx][j] === "PLACEHOLDER" ) { + break; + } + } + var myColCount = colCount; + matrix[rowIdx][colCount++] = $th.text(); + + // ----- If title cell has colspan, add empty titles for extra cell width. + for ( var j = 1; j < colspan; j++ ) { + matrix[rowIdx][colCount++] = ""; + } + + // ----- If title cell has rowspan, add empty titles for extra cell height. + for ( var i = 1; i < rowspan; i++ ) { + var thisRow = rowIdx+i; + if ( matrix[thisRow] === undefined ) { + matrix[thisRow] = []; + } + // First add placeholder text for any previous columns. + for ( var j = (matrix[thisRow]).length; j < myColCount; j++ ) { + matrix[thisRow][j] = "PLACEHOLDER"; + } + for ( var j = 0; j < colspan; j++ ) { // and empty for my columns + matrix[thisRow][myColCount+j] = ""; + } + } + } + } + + return matrix; + }, /** * Get data from DataTables' internals and format it for output @@ -1748,21 +1804,18 @@ TableTools.prototype = { */ if ( oConfig.bHeader ) { - aRow = []; - - for ( i=0, iLen=dt.aoColumns.length ; i/g, "" ).replace(/^\s+|\s+$/g,""); + var headerMatrix = this._fnGetHeaders(); + for ( var rowIdx = 0; rowIdx < headerMatrix.length; rowIdx++ ) { + aRow = []; + for ( var colIdx = 0; colIdx < (headerMatrix[rowIdx]).length; colIdx++ ) { + sLoopData = headerMatrix[rowIdx][colIdx].replace(/\n/g," ").replace( /<.*?>/g, "" ).replace(/^\s+|\s+$/g,""); sLoopData = this._fnHtmlDecode( sLoopData ); aRow.push( this._fnBoundData( sLoopData, oConfig.sFieldBoundary, regex ) ); } + aData.push( aRow.join(oConfig.sFieldSeperator) ); } - - aData.push( aRow.join(oConfig.sFieldSeperator) ); - } + } bSelectedOnly = true;