Output: add duplicate spans option. Fixes #619

This commit is contained in:
Mottie 2014-05-20 21:40:57 -05:00
parent 063f109659
commit c429a0aa0a
2 changed files with 65 additions and 36 deletions

View File

@ -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 @@
<h3><a href="#">Notes</a></h3>
<div>
<ul>
<li>In <span class="version">v2.16.5</span>, added the <code>output_ignoreColumns</code> option &amp; modified the <code>output_callback</code> parameters.</li>
<li>In <span class="version">v2.16.5</span>,
<ul>
<li>Added the <code>output_ignoreColumns</code> option &amp; modified the <code>output_callback</code> parameters.</li>
<li>Added <code>output_duplicateSpans</code> option to duplicate (when <code>true</code>) colspan &amp; rowspan content across cells.</li>
</ul>
</li>
<li>In <span class="version">v2.16.4</span>, added the <code>output_encoding</code> option.<br><br></li>
<li>This widget will <strong>only work</strong> in tablesorter version 2.8+ and jQuery version 1.7+.</li>
<li>This widget can output the table data to:
@ -477,6 +482,28 @@ line,value1,value2,value3
</div>
</td>
</tr>
<tr id="output_duplicatespans">
<td><a href="#" class="permalink">output_duplicateSpans</a></td>
<td><code>true</code></td>
<td>
When <code>true</code>, colspan &amp; rowspan content is duplicated in the output
<div class="collapsible">
<br>
By default, any <em>tbody</em> cells that are included in the colspan or rowspan will have the cell's content duplicated in the output. When set to <code>false</code>, the cells within the colspan or rowspan will be empty.<br>
Here is an example of the second table output with this option set to <code>false</code>:
<pre class="prettyprint lang-js">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</pre>This option does not affect thead cells, they will always have duplicated content.
</div>
</td>
</tr>
<tr id="output_replacequote">
<td><a href="#" class="permalink">output_replaceQuote</a></td>
<td><code>'\u201c;'</code></td>

View File

@ -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);