Skip to content

Commit

Permalink
Output: add duplicate spans option. Fixes #619
Browse files Browse the repository at this point in the history
  • Loading branch information
Mottie committed May 21, 2014
1 parent 063f109 commit c429a0a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
61 changes: 44 additions & 17 deletions docs/example-widget-output.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,'

}
});
Expand Down Expand Up @@ -224,7 +224,12 @@ <h3>Flexible client-side table sorting</h3>
<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:
Expand Down Expand Up @@ -477,6 +482,28 @@ <h4>Output widget default options (added inside of tablesorter <code>widgetOptio
</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>
Expand Down
40 changes: 21 additions & 19 deletions js/widgets/widget-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -69,19 +70,19 @@ 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 : '';
}
}
}

// 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++;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c429a0a

Please sign in to comment.