Created
November 24, 2017 13:45
-
-
Save Noy/5003358154a52fa4f164ff3e467d6c02 to your computer and use it in GitHub Desktop.
This file contains 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
$(document).ready(function () { | |
function exportTableToCSV($table, filename) { | |
var $rows = $table.find('tr:has(td),tr:has(th)'), | |
tmpColDelim = String.fromCharCode(11), | |
tmpRowDelim = String.fromCharCode(0), | |
colDelim = '","', | |
rowDelim = '"\r\n"', | |
csv = '"' + $rows.map(function (i, row) { | |
var $row = $(row), $cols = $row.find('td,th'); | |
return $cols.map(function (j, col) { | |
var $col = $(col), text = $col.text(); | |
return text.replace(/"/g, '""'); // escape double quotes | |
}).get().join(tmpColDelim); | |
}).get().join(tmpRowDelim) | |
.split(tmpRowDelim).join(rowDelim) | |
.split(tmpColDelim).join(colDelim) + '"', | |
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); | |
if (window.navigator.msSaveBlob) { | |
window.navigator.msSaveOrOpenBlob(new Blob([csv], {type: "text/plain;charset=utf-8;"}), "csvname.csv") | |
} | |
else { | |
$(this).attr({ 'download': filename, 'href': csvData, 'target': '_blank' }); | |
} | |
} | |
$("#export").on('click', function (event) { | |
exportTableToCSV.apply(this, [$('#MainTable'), 'emails.csv']); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment