1

Am new to exporting html table to csv. The code below could export only the current visible page, but I want to be able to download from pages 1 -- 200 for instance at a click of a button.

// download html table to csv file
function downloadCSV(csv, filename) {
  var csvFile;
  var downloadLink;

  csvFile = new Blob([csv], {
    type: "text/csv"
  });

  downloadLink = document.createElement("a");
  downloadLink.download = filename;
  downloadLink.href = window.URL.createObjectURL(csvFile);

  downloadLink.style.display = "none";

  document.body.appendChild(downloadLink);

  downloadLink.click();
}

function exportTableToCSV(filename) {
  var csv = [];
  var rows = document.querySelectorAll("table tr");

  for (var i = 0; i < rows.length; i++) {
    var row = [],
      cols = rows[i].querySelectorAll("td, th");
    for (var j = 0; j < cols.length; j++)
      row.push(cols[j].innerText);

    csv.push(row.join(","));
  }
  //download csv file
  downloadCSV(csv.join("\n"), filename);

}
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Danny76
  • 29
  • 4
  • How the pagination works? client? server? – Mosh Feu Jan 15 '18 at 11:30
  • Hi Mosh the pagination works from client side. – Danny76 Jan 16 '18 at 09:00
  • So you can display all the rows for a moment, generate the csv and hide them again. Or re-generate the table but without pagination and export it. Show me the code that generate the table.. – Mosh Feu Jan 16 '18 at 11:56
  • I don't know whether you have come across this issue before. Somehow I don't know why my next or previous button for my table is not doing anything. The result still remains the same allthrough and there is no changes. For example when I search so many genes, lets say up to 100 pages, when I click next page the result is the same as the previous all through the 100 pages. Please help me out. – Danny76 Jan 18 '18 at 10:25
  • Table in search.html ```
    header1 header2
    {% if result.object.item1 %} {% endif %}
    {{result.object.item1}} {{result.object.item2}} D
    ```
    – Danny76 Jan 18 '18 at 10:34
  • What is an angular app? – Mosh Feu Jan 18 '18 at 12:10

0 Answers0