Spaces:
Running
Running
Commit
·
b4abe72
1
Parent(s):
152735e
fixing bug for downloading
Browse files- templates/data.html +22 -19
templates/data.html
CHANGED
|
@@ -100,22 +100,25 @@
|
|
| 100 |
const table = document.querySelector(`#content${selectedTableIndex} table`);
|
| 101 |
|
| 102 |
if (!table) {
|
| 103 |
-
|
| 104 |
-
|
| 105 |
}
|
| 106 |
|
| 107 |
// Get table data
|
| 108 |
let csv = [];
|
| 109 |
-
const rows = table.querySelectorAll('tr');
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
// Combine CSV rows
|
| 121 |
const csvContent = csv.join('\n');
|
|
@@ -126,15 +129,15 @@
|
|
| 126 |
// Create a download link
|
| 127 |
const link = document.createElement('a');
|
| 128 |
if (link.download !== undefined) {
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
}
|
| 137 |
-
|
| 138 |
|
| 139 |
// ... rest of the existing code ...
|
| 140 |
</script>
|
|
|
|
| 100 |
const table = document.querySelector(`#content${selectedTableIndex} table`);
|
| 101 |
|
| 102 |
if (!table) {
|
| 103 |
+
console.error('No table found');
|
| 104 |
+
return;
|
| 105 |
}
|
| 106 |
|
| 107 |
// Get table data
|
| 108 |
let csv = [];
|
| 109 |
+
const rows = table.querySelectorAll('thead tr, tbody tr');
|
| 110 |
|
| 111 |
+
rows.forEach(row => {
|
| 112 |
+
const cells = row.querySelectorAll('th, td');
|
| 113 |
+
let rowData = [];
|
| 114 |
+
cells.forEach(cell => {
|
| 115 |
+
// Get the text content, removing any extra whitespace
|
| 116 |
+
let text = cell.textContent.trim().replace(/\s\s+/g, ' ');
|
| 117 |
+
// Escape double quotes and enclose fields in quotes
|
| 118 |
+
rowData.push('"' + text.replace(/"/g, '""') + '"');
|
| 119 |
+
});
|
| 120 |
+
csv.push(rowData.join(','));
|
| 121 |
+
});
|
| 122 |
|
| 123 |
// Combine CSV rows
|
| 124 |
const csvContent = csv.join('\n');
|
|
|
|
| 129 |
// Create a download link
|
| 130 |
const link = document.createElement('a');
|
| 131 |
if (link.download !== undefined) {
|
| 132 |
+
const url = URL.createObjectURL(blob);
|
| 133 |
+
link.setAttribute('href', url);
|
| 134 |
+
link.setAttribute('download', 'table_data.csv');
|
| 135 |
+
link.style.visibility = 'hidden';
|
| 136 |
+
document.body.appendChild(link);
|
| 137 |
+
link.click();
|
| 138 |
+
document.body.removeChild(link);
|
| 139 |
}
|
| 140 |
+
}
|
| 141 |
|
| 142 |
// ... rest of the existing code ...
|
| 143 |
</script>
|