-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add download as csv (with semicolon seperator)
- Loading branch information
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,34 @@ | ||
// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification | ||
// Please see documentation at https://docs.microsoft.com/;aspnet/core/client-side/bundling-and-minification | ||
// for details on configuring this project to bundle and minify static web assets. | ||
|
||
// Write your JavaScript code. | ||
// Quick and simple export target #table_id into a csv | ||
function download_table_as_csv(table_id, separator = ';') { | ||
// Select rows from table_id | ||
var rows = document.querySelectorAll('table#' + table_id + ' tr'); | ||
// Construct csv | ||
var csv = []; | ||
for (var i = 0; i < rows.length; i++) { | ||
var row = [], cols = rows[i].querySelectorAll('td, th'); | ||
for (var j = 0; j < cols.length; j++) { | ||
// Clean innertext to remove multiple spaces and jumpline (break csv) | ||
var data = cols[j].innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ') | ||
// Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv) | ||
data = data.replace(/"/g, '""'); | ||
// Push escaped string | ||
row.push('"' + data + '"'); | ||
} | ||
csv.push(row.join(separator)); | ||
} | ||
var csv_string = csv.join('\n'); | ||
// Download it | ||
var filename = 'export_' + table_id + '_' + new Date().toLocaleDateString() + '.csv'; | ||
var link = document.createElement('a'); | ||
link.style.display = 'none'; | ||
link.setAttribute('target', '_blank'); | ||
link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string)); | ||
link.setAttribute('download', filename); | ||
document.body.appendChild(link); | ||
link.click(); | ||
document.body.removeChild(link); | ||
} |