From 6da3aaa5cee10a2f77cd15e798dc261b25bd319a Mon Sep 17 00:00:00 2001 From: mtyszler Date: Sat, 25 Jan 2020 20:58:18 +0100 Subject: [PATCH 01/19] Include html example with the "original" buttons of Datatables, show casing the problem with export complex headers and footers --- .../example_DatatablesButtons_original.html | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 test/complex_headers_and_footers/example_DatatablesButtons_original.html diff --git a/test/complex_headers_and_footers/example_DatatablesButtons_original.html b/test/complex_headers_and_footers/example_DatatablesButtons_original.html new file mode 100644 index 00000000..5837d062 --- /dev/null +++ b/test/complex_headers_and_footers/example_DatatablesButtons_original.html @@ -0,0 +1,165 @@ + + + + + + DataTables example + + + + + + + + + + + + + + + + + + +
+
+

DataTables example, using original libraries

+

Based on https://github.com/DataTables/DataTables/blob/master/examples/basic_init/zero_configuration.html

+

an on https://datatables.net/extensions/buttons/config

+
+

This example is intended for testing the export buttons when headers and or footers are complex and column visibility is potentially used.

+

Here, the first row of headers and last row of the footer does not get exported.

+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameBlock 1Block 2
PositionOfficeAgeStart dateSalary
Tiger NixonSystem ArchitectEdinburgh612011/04/25$320,800
Garrett WintersAccountantTokyo632011/07/25$170,750
Ashton CoxJunior Technical AuthorSan Francisco662009/01/12$86,000
Cedric KellySenior Javascript DeveloperEdinburgh222012/03/29$433,060
Airi SatouAccountantTokyo332008/11/28$162,700
Brielle WilliamsonIntegration SpecialistNew York612012/12/02$372,000
Herrod ChandlerSales AssistantSan Francisco592012/08/06$137,500
Rhona DavidsonIntegration SpecialistTokyo552010/10/14$327,900
N8200
Second footer row, for illustration purposes only
+
+
+ + \ No newline at end of file From b86a99867c722d97bac11e843b629af33c066140 Mon Sep 17 00:00:00 2001 From: mtyszler Date: Sat, 25 Jan 2020 22:35:01 +0100 Subject: [PATCH 02/19] Introduces function to process complex headers. Based on https://github.com/rstudio/DT/pull/751/commits/ed15f2f9335cfe2b314091ac29980311fb0c2345 and https://github.com/rstudio/DT/pull/751/commits/890094de7ba4782d53a795eb595188c1eaed8060 --- js/dataTables.buttons.js | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/js/dataTables.buttons.js b/js/dataTables.buttons.js index a8c5197f..247f0bb3 100644 --- a/js/dataTables.buttons.js +++ b/js/dataTables.buttons.js @@ -1841,6 +1841,67 @@ var _message = function ( dt, option, position ) +/* ----- BEGIN added/edited Code ----- */ +/** + * getHeaders - function to read full data content based on dt and convert it into matrix + * form for exporting. + * + */ +var getHeaders = function( dt ){ + var thRows = dt.aoHeader; + var numRows = thRows.length; + var numCols = thRows[0].length; + var matrix = []; + + //initialize matrix with "_getHeaders_initialized_" string + for ( var rowIdx = 0; rowIdx < numRows; rowIdx++ ) { + matrix[rowIdx] = []; + for ( var colIdx = 0; colIdx < numCols; colIdx++ ){ + matrix[rowIdx][colIdx] = "_getHeaders_initialized_" + } + } + + // 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 all columns specified in this row. + for ( var colIdx = 0; colIdx < numCols; colIdx++ ) + { + var $th = $($row[colIdx].cell); + var colspan = $th.attr("colspan") || 1; + var rowspan = $th.attr("rowspan") || 1; + var cellIndex = $row[colIdx].cell.cellIndex; + + + // If the cell has been initialized but not changed, update + if (matrix[rowIdx][colIdx]=="_getHeaders_initialized_") { + + matrix[rowIdx][colIdx] = $th.text(); + + // rowspan + for(var expand = 1; expand < rowspan; expand++){ + matrix[rowIdx+expand][colIdx]=""; + } + + // colspan, use cellindex instead. + // colspan will be incorrect for hidden columns (colvis), but cellIndex will correctly find them + expand = 1; + if (colIdx + expand < numCols) { + var next_cellIndex = $row[colIdx+expand].cell.cellIndex; + while(cellIndex == next_cellIndex){ + matrix[rowIdx][colIdx+expand]=""; + expand++; + if (colIdx + expand >= numCols) break; + next_cellIndex = $row[colIdx+expand].cell.cellIndex; + } + } + } + } + } + return matrix; +}; +/* ----- END added/edited Code ----- */ var _exportTextarea = $('