Created
May 15, 2015 16:09
-
-
Save stephentcannon/9dfa2190a8f0c38f59e1 to your computer and use it in GitHub Desktop.
table to csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// $("table.tableCSV").toCSV(); | |
jQuery.fn.toCSV = function() { | |
var data = $(this).first(); | |
var csvData = []; | |
var tmpArr = []; | |
var tmpStr = ''; | |
data.find("tr").each(function() { | |
if($(this).find("th").length) { | |
$(this).find("th").each(function() { | |
tmpStr = $(this).text().replace(/"/g, '""'); | |
tmpArr.push('"' + tmpStr + '"'); | |
}); | |
csvData.push(tmpArr); | |
} else { | |
tmpArr = []; | |
$(this).find("td").each(function() { | |
if($(this).text().match(/^-{0,1}\d*\.{0,1}\d+$/)) { | |
tmpArr.push(parseFloat($(this).text())); | |
} else { | |
tmpStr = $(this).text().replace(/"/g, '""'); | |
tmpArr.push('"' + tmpStr + '"'); | |
} | |
}); | |
csvData.push(tmpArr.join(',')); | |
} | |
}); | |
var output = csvData.join('\n'); | |
var uri = 'data:application/csv;charset=UTF-8,' + encodeURIComponent(output); | |
window.open(uri); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment