From c429a0aa0a26252ca76198bb7ada6c307b4486e6 Mon Sep 17 00:00:00 2001 From: Mottie Date: Tue, 20 May 2014 21:40:57 -0500 Subject: [PATCH] Output: add duplicate spans option. Fixes #619 --- docs/example-widget-output.html | 61 ++++++++++++++++++++++++--------- js/widgets/widget-output.js | 40 +++++++++++---------- 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/docs/example-widget-output.html b/docs/example-widget-output.html index ea46fabb..5338e152 100644 --- a/docs/example-widget-output.html +++ b/docs/example-widget-output.html @@ -117,30 +117,30 @@ widgetOptions : { filter_filteredRow : 'filtered', filter_reset : demos[groupIndex] + ' .reset', - - output_separator : ',', // ',' 'json', 'array' or separator (e.g. ',') - output_ignoreColumns: [], // columns to ignore [0, 1,... ] (zero-based index) - output_dataAttrib : 'data-name', // data-attribute containing alternate cell text - output_headerRows : true, // output all header rows (multiple rows) - output_delivery : 'p', // (p)opup, (d)ownload - output_saveRows : 'f', // (a)ll, (f)iltered or (v)isible - output_replaceQuote : '\u201c;', // change quote to left double quote - output_includeHTML : true, // output includes all cell HTML (except the header cells) - output_trimSpaces : false, // remove extra white-space characters from beginning & end - output_wrapQuotes : false, // wrap every cell output in quotes - output_popupStyle : 'width=580,height=310', - output_saveFileName : 'mytable.csv', + output_separator : ',', // ',' 'json', 'array' or separator (e.g. ',') + output_ignoreColumns : [], // columns to ignore [0, 1,... ] (zero-based index) + output_dataAttrib : 'data-name', // data-attribute containing alternate cell text + output_headerRows : true, // output all header rows (multiple rows) + output_delivery : 'p', // (p)opup, (d)ownload + output_saveRows : 'f', // (a)ll, (f)iltered or (v)isible + output_duplicateSpans: true, // duplicate output data in tbody colspan/rowspan + output_replaceQuote : '\u201c;', // change quote to left double quote + output_includeHTML : true, // output includes all cell HTML (except the header cells) + output_trimSpaces : false, // remove extra white-space characters from beginning & end + output_wrapQuotes : false, // wrap every cell output in quotes + output_popupStyle : 'width=580,height=310', + output_saveFileName : 'mytable.csv', // callbackJSON used when outputting JSON & any header cells has a colspan - unique names required - output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + cellIndex + ')'; }, + output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + cellIndex + ')'; }, // callback executed when processing completes // return true to continue download/output // return false to stop delivery & do something else with the data - output_callback : function(config, data) { return true; }, + output_callback : function(config, data) { return true; }, // output data type (with BOM or Windows-1252 is needed for excel) // NO BOM : 'data:text/csv;charset=utf8,' // With BOM : 'data:text/csv;charset=utf8,%EF%BB%BF' // WIN 1252 : 'data:text/csv;charset=windows-1252' - output_encoding : 'data:text/csv;charset=utf8,' + output_encoding : 'data:text/csv;charset=utf8,' } }); @@ -224,7 +224,12 @@

Notes

+ + + true + + When true, colspan & rowspan content is duplicated in the output +
+
+ By default, any tbody cells that are included in the colspan or rowspan will have the cell's content duplicated in the output. When set to false, the cells within the colspan or rowspan will be empty.
+ Here is an example of the second table output with this option set to false: +
line,values,values,values
+line,value1,value2,value3
+1,1.1,1.2,1.3
+,1.4,1.5,
+2,2.1,2.2,2.3
+,2.4,2.5,
+3,3.1,3.2,3.3
+,3.4,3.5,
+4,4.1,,4.2
+,,,4.3
This option does not affect thead cells, they will always have duplicated content. +
+ + '\u201c;' diff --git a/js/widgets/widget-output.js b/js/widgets/widget-output.js index 76e6c47e..fad2c1ac 100644 --- a/js/widgets/widget-output.js +++ b/js/widgets/widget-output.js @@ -42,6 +42,7 @@ output = ts.output = { var $this, row, col, rowlen, collen, txt, wo = c.widgetOptions, tmpRow = [], + dupe = wo.output_duplicateSpans, addSpanIndex = isHeader && isJSON && wo.output_headerRows && $.isFunction(wo.output_callbackJSON), cellIndex = 0; $rows.each(function(rowIndex) { @@ -55,7 +56,7 @@ output = ts.output = { txt = output.formatData( wo, $this.attr(wo.output_dataAttrib) || $this.html(), isHeader ); for (row = 1; row <= rowlen; row++) { if (!tmpRow[rowIndex + row]) { tmpRow[rowIndex + row] = []; } - tmpRow[rowIndex + row][cellIndex] = txt; + tmpRow[rowIndex + row][cellIndex] = isHeader ? txt : dupe ? txt : ''; } } // process colspans @@ -69,11 +70,11 @@ output = ts.output = { for (row = 0; row < rowlen; row++) { if (!tmpRow[rowIndex + row]) { tmpRow[rowIndex + row] = []; } tmpRow[rowIndex + row][cellIndex + col] = addSpanIndex ? - wo.output_callbackJSON($this, txt, cellIndex + col) || txt + '(' + (cellIndex + col) + ')' : txt; + wo.output_callbackJSON($this, txt, cellIndex + col) || txt + '(' + (cellIndex + col) + ')' : isHeader ? txt : dupe ? txt : ''; } } else { tmpRow[rowIndex][cellIndex + col] = addSpanIndex ? - wo.output_callbackJSON($this, txt, cellIndex + col) || txt + '(' + (cellIndex + col) + ')' : txt; + wo.output_callbackJSON($this, txt, cellIndex + col) || txt + '(' + (cellIndex + col) + ')' : isHeader ? txt : dupe ? txt : ''; } } } @@ -81,7 +82,7 @@ output = ts.output = { // don't include hidden columns if ( $this.css('display') !== 'none' ) { // skip column if already defined - while (tmpRow[rowIndex][cellIndex]) { cellIndex++; } + while (typeof tmpRow[rowIndex][cellIndex] !== 'undefined') { cellIndex++; } tmpRow[rowIndex][cellIndex] = tmpRow[rowIndex][cellIndex] || output.formatData( wo, $this.attr(wo.output_dataAttrib) || $this.html(), isHeader ); cellIndex++; @@ -258,29 +259,30 @@ output = ts.output = { ts.addWidget({ id: "output", options: { - output_separator : ',', // set to "json", "array" or any separator - output_ignoreColumns: [], // columns to ignore [0, 1,... ] (zero-based index) - output_dataAttrib : 'data-name', // header attrib containing modified header name - output_headerRows : false, // if true, include multiple header rows (JSON only) - output_delivery : 'popup', // popup, download - output_saveRows : 'filtered', // all, visible or filtered - output_replaceQuote : '\u201c;', // left double quote - output_includeHTML : false, - output_trimSpaces : true, - output_wrapQuotes : false, - output_popupStyle : 'width=500,height=300', - output_saveFileName : 'mytable.csv', + output_separator : ',', // set to "json", "array" or any separator + output_ignoreColumns : [], // columns to ignore [0, 1,... ] (zero-based index) + output_dataAttrib : 'data-name', // header attrib containing modified header name + output_headerRows : false, // if true, include multiple header rows (JSON only) + output_delivery : 'popup', // popup, download + output_saveRows : 'filtered', // all, visible or filtered + output_duplicateSpans: true, // duplicate output data in tbody colspan/rowspan + output_replaceQuote : '\u201c;', // left double quote + output_includeHTML : false, + output_trimSpaces : true, + output_wrapQuotes : false, + output_popupStyle : 'width=500,height=300', + output_saveFileName : 'mytable.csv', // callback executed when processing completes // return true to continue download/output // return false to stop delivery & do something else with the data - output_callback : function(config, data){ return true; }, + output_callback : function(config, data){ return true; }, // JSON callback executed when a colspan is encountered in the header - output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + (cellIndex) + ')'; }, + output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + (cellIndex) + ')'; }, // output data type (with BOM or Windows-1252 is needed for excel) // NO BOM : 'data:text/csv;charset=utf8,' // With BOM : 'data:text/csv;charset=utf8,%EF%BB%BF' // WIN 1252 : 'data:text/csv;charset=windows-1252' - output_encoding : 'data:text/csv;charset=utf8,' + output_encoding : 'data:text/csv;charset=utf8,' }, init: function(table, thisWidget, c) { output.init(c);