Skip to content

Instantly share code, notes, and snippets.

@zackdever
Created April 26, 2013 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackdever/5464832 to your computer and use it in GitHub Desktop.
Save zackdever/5464832 to your computer and use it in GitHub Desktop.
converts an html table to a csv string, and makes it available as a file download
function convertTableToCsv(tableSelector) {
return _.map($(tableSelector + ' tr'), function(row) {
return _.map($(row).find('th, td'), function(datum) {
return datum.textContent.replace(/,/g, '');
}).join(',');
}).join('\n');
}
// to download
var data = convertTableToCsv('#mytable');
window.location='data:text/csv;charset=utf8,' + encodeURIComponent(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment