The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Download CSV - Front End
This is a simple demo to allow users to download a table of content as a CSV from the front end.It can be used in a View or any other page / post to export any tables as CSV files.Step 1: Add / Upload JS FileThe following JavaScript code contains 2 functions, named downloadCSV() and exportTableToCSV().The downloadCSV() function takes CSV data and generates a download link to download an HTML table data in a CSV file.
function downloadCSV(csv, filename) { var csvFile; var downloadLink; // CSV file csvFile = new Blob([csv], {type: "text/csv"}); // Download link downloadLink = document.createElement("a"); // File name downloadLink.download = filename; // Create a link to the file downloadLink.href = window.URL.createObjectURL(csvFile); // Hide download link downloadLink.style.display = "none"; // Add the link to DOM document.body.appendChild(downloadLink); // Click download link downloadLink.click(); }
The exportTableToCSV() function creates CSV data from table HTML and download CSV data as a file by using the downloadCSV() function.
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); }
Step 2: TableThe members HTML table below contains some basic users data, like name, email, country. You can a view to create this type of table for each of your entries quite easily.
<table><tbody><tr><th>Name</th><th>Email</th><th>Country</th></tr><tr><td>John Doe</td><td>[email protected]</td><td>USA</td></tr><tr><td>Stephen Thomas</td><td>[email protected]</td><td>UK</td></tr><tr><td>Natly Oath</td><td>[email protected]</td><td>France</td></tr></tbody></table>
On clicking the button, exportTableToCSV() method is called to export table data to CSV file. Also, the desired filename for download CSV file is passed to this function.
<button onclick="exportTableToCSV('members.csv')">Export HTML Table To CSV File</button>
You can view a demo of how this works, along with instructions on how to implement this in your own Views, HERE.
July 5, 2018 at 12:40 am
Brilliant - I was just looking for this and will go it a go shortly. Huge thanks for sharing !
Best wishes,
Stephen
July 5, 2018 at 5:18 am
Hi,
Is it possible in the view to show the title of the form please and if so how - many thanks for the help !
Best wishes,
Stephen
July 9, 2018 at 3:32 am
Hi Stephen,
The script is designed to only export the table and any contents within.
The easiest solution would be to add a new row to the top of the table with the name included.
You can hide this on the front end using some CSS if you don't want it included on the page but it will be included in the export.
Hope that helps.
Chris
July 9, 2018 at 8:38 am
Many thanks Chris - much appreciated
November 6, 2018 at 4:26 am
I tried loading this demo on my site and the export puts all the info on one row of the csv unlike copying the format of the table like your demo. any idea why this might happen?
November 6, 2018 at 4:40 am
Hi Hunter,
Do you have a link to your version or a screenshot?
The only issue i've found similar to yours is when using the Table Sorter plugin it adds all of the table headers into one cell in the CSV.
Thanks
Chris
November 7, 2018 at 7:50 am
Weird, right?
Attachment:
Discussion closed.