I am working with an HTML table (table id='testTable') and a button in the HTML code:
<button id="btnExport" onclick="javascript:xport.toCSV('testTable');">CSV</button>
There is also JavaScript code involved:
toCSV: function(tableId, filename) {
var date=new Date();
this._filename = (typeof filename === 'undefined') ? tableId : filename;
// Generating CSV string from HTML Table
var csv = this._tableToCSV(document.getElementById(tableId));
// Creating a CSV Blob
var blob = new Blob([csv], { type: "text/csv" });
// Determining the download approach
if (navigator.msSaveOrOpenBlob) {
// Works for Internet Explorer and Microsoft Edge
navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
} else {
this._downloadAnchor(URL.createObjectURL(blob), 'csv');
}
}
_tableToCSV: function(table) {
// Using `slice` to create arrays
var slice = Array.prototype.slice;
return slice
.call(table.rows)
.map(function(row) {
return slice
.call(row.cells)
.map(function(cell) {
return '"t"'.replace("t", cell.textContent);
})
.join(",");
})
.join("\r\n");
}
I want to update the filename to the current date. How can I achieve that?