Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix header rendering when exporting csv with multiple header rows #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions js/dataTables.tableTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
}
} );
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2186,21 +2239,18 @@ TableTools.prototype = {
*/
if ( oConfig.bHeader )
{
aRow = [];

for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
{
if ( aColumnsInc[i] )
{
sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g," ").replace( /<.*?>/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;

Expand Down
73 changes: 63 additions & 10 deletions src/TableTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1748,21 +1804,18 @@ TableTools.prototype = {
*/
if ( oConfig.bHeader )
{
aRow = [];

for ( i=0, iLen=dt.aoColumns.length ; i<iLen ; i++ )
{
if ( aColumnsInc[i] )
{
sLoopData = dt.aoColumns[i].sTitle.replace(/\n/g," ").replace( /<.*?>/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;

Expand Down