Skip to content

Commit

Permalink
Add ability to name the CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
luke-strange committed Jul 8, 2024
1 parent 26418cc commit d7a9d5a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/assets/js/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ function downloadCSV() {
for (const player in playerStats) {
csv += `${player},${playerStats[player].assists},${playerStats[player].goals},${playerStats[player].blocks}\n`;
}

const fileName = document.getElementById('fileName').value
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('hidden', '');
a.setAttribute('href', url);
a.setAttribute('download', 'player_stats.csv');
if (!fileName){
a.setAttribute('download', 'player_stats.csv');
}
else {
a.setAttribute('download', fileName)
}
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
Expand Down
6 changes: 4 additions & 2 deletions src/index.vto
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ title: Fris Stats
</div>

<h2>Player Stats Table</h2>
<button onclick="downloadCSV()">Download as CSV</button>
<table id="statsTable">
<thead>
<tr>
Expand All @@ -35,5 +34,8 @@ title: Fris Stats
<tbody>
</tbody>
</table>

<form id="csvName">
<label for="fileName">Enter a filename (e.g. game_1_leeds_open):</label>
<input type="text" id="fileName" name="fileName" required>
<button onclick="downloadCSV()">Download as CSV</button>
<script src="assets/js/test.js"></script>

0 comments on commit d7a9d5a

Please sign in to comment.