Files

44 lines
1.7 KiB
JavaScript

jQuery(document).ready(function($) {
var uniqueId = SIGEExportVars.unique_id;
$("#export-csv").click(function(e) {
e.preventDefault();
var $table = $("#enrollments-table" + uniqueId);
// Get all headers
var headers = [];
$table.find("thead th").each(function() {
headers.push($(this).text().trim());
});
// Start CSV content with headers
var csvContent = headers.map(h => '"' + h.replace(/"/g, '""') + '"').join(",") + "\n";
// Get all rows
$table.find("tbody tr").each(function() {
var row = [];
$(this).find("td").each(function() {
row.push('"' + $(this).text().trim().replace(/"/g, '""') + '"');
});
csvContent += row.join(",") + "\n";
});
// Create and trigger download
var now = new Date();
var formattedDate = `${("0" + now.getDate()).slice(-2)}_${("0" + (now.getMonth() + 1)).slice(-2)}_${now.getFullYear()}_${("0" + now.getHours()).slice(-2)}_${("0" + now.getMinutes()).slice(-2)}_${("0" + now.getSeconds()).slice(-2)}`;
var filename = `enrollments_table_${formattedDate}.csv`;
var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
var link = document.createElement("a");
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, filename);
} else {
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
});
});