3

I have html table (table id='testTable') and a button in html:

 <button id="btnExport" onclick="javascript:xport.toCSV('testTable');">CSV</button>

and javascript code:

toCSV: function(tableId, filename) {
var date=new Date();
this._filename = (typeof filename === 'undefined') ? tableId :      filename;
// Generate our CSV string from out HTML Table
var csv = this._tableToCSV(document.getElementById(tableId));
// Create a CSV Blob
var blob = new Blob([csv], { type: "text/csv" });

// Determine which approach to take for the download
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) {
// We'll be co-opting `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 change the filename to current date how can I do that?

2 Answers 2

1

first change toCSV to:

toCSV: function(tableId, filename) {
    var date = new Date();
    this._filename = (typeof filename === 'undefined') ? date : filename;
    // Generate our CSV string from out HTML Table
    var csv = this._tableToCSV(document.getElementById(tableId));
    // Create a CSV Blob
    var blob = new Blob([csv], { type: "text/csv" });

    // Determine which approach to take for the download
    if (navigator.msSaveOrOpenBlob) {
        // Works for Internet Explorer and Microsoft Edge
        navigator.msSaveOrOpenBlob(blob, this._filename + ".csv");
    } else {
        this._downloadAnchor(URL.createObjectURL(blob), 'csv',this._filename);
    }
}

second change _downloadAnchor to:

_downloadAnchor: function(content, ext, filename) {
    var anchor = document.createElement("a");
    anchor.style = "display:none !important";
    anchor.id = "downloadanchor";
    document.body.appendChild(anchor);

    // If the [download] attribute is supported, try to use it

    if ("download" in anchor) {
        anchor.download = filename + "." + ext;
    }
    anchor.href = content;
    anchor.click();
    anchor.remove();
}
Sign up to request clarification or add additional context in comments.

4 Comments

you need show function: _tableToCSV ,,,,the problem is the function,i think
I found the problem I think.Button toCSV('testTable') and it returns filename testTable.How can I add date to filename
@Nur, still need function _downloadAnchor,you can try the code with ms browser first
@Nur key is _downloadAnchor ,need to know
0
  _downloadAnchor: function(content, ext) {
  var anchor = document.createElement("a");
  anchor.style = "display:none !important";
  anchor.id = "downloadanchor";
  document.body.appendChild(anchor);

  // If the [download] attribute is supported, try to use it

  if ("download" in anchor) {
    anchor.download = this._filename + "." + ext;
  }
  anchor.href = content;
  anchor.click();
  anchor.remove();
   }

1 Comment

@xianshenglu this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.