Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Created April 10, 2015 19:27
Show Gist options
  • Save Crisfole/2e4afdf4c7fcfb387e30 to your computer and use it in GitHub Desktop.
Save Crisfole/2e4afdf4c7fcfb387e30 to your computer and use it in GitHub Desktop.
results.html
{{#if results}}
<header><h3>Results</h3></header>
<div><button on-click="saveCSV()">Export As CSV</button></div>
{{#each resultSets}}
<div class='resultset'>
<h4>
<span>{{db}}</span>
{{#if statistics.SelectRows || statistics.SelectCount}}
<span class='counts'>{{statistics.SelectRows}} row(s) selected with {{statistics.SelectCount}} select(s)</span>
{{/if}}
{{#if statistics.IudRows || statistics.IudCount}}
<span class='counts'>{{statistics.IudRows}} row(s) updated/inserted/deleted with {{statistics.IudCount}} update(s)/insert(s)/delete(s)</span>
{{/if}}
</h4>
{{#if errorMessages}}
<div class="error">
{{#errorMessages}}
<p>{{.}}</p>
{{/errorMessages}}
</div>
{{/if}}
{{#if messages}}
<div class="notice">
{{#messages}}
<p>{{.}}</p>
{{/messages}}
</div>
{{/if}}
{{#each data}}
{{#if _.any(headers) || _.any(rows)}}
<table>
<thead>
<tr>
{{#headers}}
<th>{{.}}</th>
{{/headers}}
</tr>
</thead>
<tbody>
{{#rows}}
<tr>
{{#each .}}
<td>{{ formatCell(.)}}</td>
{{/each}}
</tr>
{{/rows}}
</tbody>
</table>
{{/if}}
{{/each}}
</div>
{{/each}}
{{/if}}
<style>
h4 .counts {
font-size: small;
color: #888;
margin-left: .5em;
}
.resultset + .resultset {
margin-top: 3em;
}
table td, table th {
white-space: pre-line;
text-align: left;
}
</style>
<script type='text/javascript'>
var _ = require('_');
component.exports = {
data: function () {
return {
_: _,
formatCell: function (cell) {
return _.isObject(cell) ? JSON.stringify(cell) : cell;
},
};
},
computed: {
resultSets: function () {
var results = this.get('results');
return _.chain(results)
.map(function (rset, db) {
return _.extend({}, rset, { db: db });
})
.value();
},
},
saveCSV: function () {
var i, data, makeRow, text;
data = _.chain(this.get('results'))
.map(function (dbResults, db) {
var dbRows = [[db]];
if (dbResults.errorMessages.length) {
dbRows.push(["Errors"]);
for (i = 0; i < dbResults.errorMessages.length; i++) {
dbRows.push([dbResults.errorMessages[i]]);
}
dbRows.push([]);
}
if (dbResults.messages.length) {
dbRows.push(["Messages"]);
for (i = 0; i < dbResults.messages.length; i++) {
dbRows.push([dbResults.errorMessages[i]]);
}
dbRows.push([]);
}
_.each(dbResults.data, function (resultSet) {
dbRows.push(resultSet.headers);
_.each(resultSet.rows, function (row) {
dbRows.push(row);
});
});
dbRows.push([]);
return dbRows;
})
.flatten()
.value();
makeRow = function (el) {
if (el == null) el = "";
el = el.toString();
return '"' + el.replace('"', '""') + '"';
};
text = _.map(data, function (row) {
return row.map(makeRow).join(',');
}).join("\n").trim();
window.app.saveFile(text, ".csv");
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment