Output: Add output_formatContent callback

See http://stackoverflow.com/q/31457323/145346
This commit is contained in:
Mottie 2015-07-17 13:13:32 -05:00
parent 8e5f1f391e
commit 19e914fbb3
3 changed files with 49 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@ -237,6 +237,7 @@
<h4>Changes</h4>
<ul>
<li>In <span class="version">v2.22.4</span>, added <code>output_formatContent</code> callback function which allows for extra formatting of cell content (e.g. convert <code>'&amp;amp;'</code> &rarr; <code>'&amp;'</code>).</li>
<li>In <span class="version">v2.22.2</span>,
<ul>
<li>The data-attribute used by the <code>output_dataAttrib</code> can now be an empty string.</li>
@ -698,6 +699,35 @@ line,value1,value2,value3
<td><code>'mytable.csv'</code></td>
<td>When downloading, this option sets the filename of the output.</td>
</tr>
<tr id="output_formatcontent">
<td><a href="#" class="permalink">output_formatContent</a></td>
<td>null</td>
<td>This callback is executed after the content of a table cell has been processed.
<div class="collapsible">
<br>
Default setting &amp; how to use this option:
<pre class="prettyprint lang-js">output_formatContent : function( c, wo, data ) {
// data.isHeader (boolean) = true if processing a header cell
// data.$cell = jQuery object of the cell currently being processed
// data.content = processed cell content (spaces trimmed, quotes added/replaced, etc)
// **********
// use data.$cell.html() to get the original cell content
return data.content
.replace(/fred/ig, '') // because we don't like fred
// because HTML is being processed, you might want to also replace HTML codes
.replace(/&amp;amp;/g, '&') // convert '&amp;amp;' &rarr; '&amp;'
}</pre>
If you want to replace all HTML codes, check out the <a href="https://github.com/mathiasbynens/he#hedecodehtml-options">he library</a>; then you can use this code (Fred would be happy):
<pre class="prettyprint lang-js">output_formatContent : function( c, wo, data ) {
// replace all HTML shortcut codes
// (e.g. 'foo &amp;copy; bar &amp;ne; baz &amp;#x1D306; qux' &rarr; 'foo &copy; bar &ne; baz &#x1D306; qux' )
return he.decode( data.content );
}</pre>
</div>
</td>
</tr>
<tr id="output_callback">
<td><a href="#" class="permalink">output_callback</a></td>
<td>{see description}</td>

View File

@ -58,7 +58,7 @@ output = ts.output = {
// process rowspans
if ($cell.filter('[rowspan]').length) {
rowspanLen = parseInt( $cell.attr('rowspan'), 10) - 1;
txt = output.formatData( wo, $cell, isHeader );
txt = output.formatData( c, wo, $cell, isHeader );
for (row = 1; row <= rowspanLen; row++) {
if (!tmpRow[rowIndex + row]) { tmpRow[rowIndex + row] = []; }
tmpRow[rowIndex + row][cellIndex] = isHeader ? txt : dupe ? txt : '';
@ -68,7 +68,7 @@ output = ts.output = {
if ($cell.filter('[colspan]').length) {
colspanLen = parseInt( $cell.attr('colspan'), 10) - 1;
// allow data-attribute to be an empty string
txt = output.formatData( wo, $cell, isHeader );
txt = output.formatData( c, wo, $cell, isHeader );
for (col = 1; col <= colspanLen; col++) {
// if we're processing the header & making JSON, the header names need to be unique
if ($cell.filter('[rowspan]').length) {
@ -91,7 +91,7 @@ output = ts.output = {
while (typeof tmpRow[rowIndex][cellIndex] !== 'undefined') { cellIndex++; }
tmpRow[rowIndex][cellIndex] = tmpRow[rowIndex][cellIndex] ||
output.formatData( wo, $cell, isHeader );
output.formatData( c, wo, $cell, isHeader );
cellIndex++;
}
}
@ -219,7 +219,7 @@ output = ts.output = {
return json;
},
formatData : function(wo, $el, isHeader) {
formatData : function(c, wo, $el, isHeader) {
var attr = $el.attr(wo.output_dataAttrib),
txt = typeof attr !== 'undefined' ? attr : $el.html(),
quotes = (wo.output_separator || ',').toLowerCase(),
@ -242,7 +242,18 @@ output = ts.output = {
result = wo.output_trimSpaces || isHeader ? $.trim(result) : result;
// JSON & array outputs don't need quotes
quotes = separator ? false : wo.output_wrapQuotes || wo.output_regex.test(result) || output.regexQuote.test(result);
return quotes ? '"' + result + '"' : result;
result = quotes ? '"' + result + '"' : result;
// formatting callback - added v2.22.4
if ( typeof wo.output_formatContent === 'function' ) {
return wo.output_formatContent( c, wo, {
isHeader : isHeader,
$cell : $el,
content : result
});
}
return result;
},
popup : function(data, style, wrap) {
@ -337,6 +348,8 @@ ts.addWidget({
output_wrapQuotes : false,
output_popupStyle : 'width=500,height=300',
output_saveFileName : 'mytable.csv',
// format $cell content callback
output_formatContent : null, // function(config, data){ return data.content; }
// callback executed when processing completes
// return true to continue download/output
// return false to stop delivery & do something else with the data