'Where is my error in exporting HTML to CSV using Javascript in Wordpress?
I have a problem with my Jquery Script and locating the error. When I export the .CSV it show up in 2 single rows vs. 5 columns. What am I missing from the script below?
jQuery(document).ready(function($) {
function exportTableToCSV($table, filename) {
var $rows = $table.find('tr');
var csvData = "";
for (var i = 0; i < $rows.length; i++) {
var $cells = $($rows[i]).children('th,td'); //header or content cells
for (var y = 0; y < $cells.length; y++) {
if (y > 0) {
csvData += ",";
}
var txt = ($($cells[y]).text()).toString().trim();
if (txt.indexOf(',') >= 0 || txt.indexOf('"') >= 0 || txt.indexOf('n') >= 0) {
txt = '"' + txt.replace(/"/g, '""') + '"';
}
csvData += txt;
}
csvData += 'n';
}
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csvData);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}
// This must be a hyperlink
$(".exportCsv").on('click', function(event) {
// CSV
exportTableToCSV.apply(this, [$('#exportData>table'), 'Export-List.csv']);
// IF CSV, don't do event.preventDefault() or return false
// We actually need this to be a typical hyperlink
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
