Spaces:
Running
Running
Commit
·
152735e
1
Parent(s):
fd40989
download functionality added
Browse files- templates/data.html +58 -17
templates/data.html
CHANGED
|
@@ -93,23 +93,64 @@
|
|
| 93 |
|
| 94 |
// Function to handle the table data download
|
| 95 |
function downloadTableData() {
|
| 96 |
-
//
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
{% endblock %}
|
|
|
|
| 93 |
|
| 94 |
// Function to handle the table data download
|
| 95 |
function downloadTableData() {
|
| 96 |
+
// Get the currently selected table index
|
| 97 |
+
const selectedTableIndex = $('#table-dropdown').val();
|
| 98 |
+
|
| 99 |
+
// Get the table element
|
| 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('tr');
|
| 110 |
+
|
| 111 |
+
for (const row of rows) {
|
| 112 |
+
const cells = row.querySelectorAll('th, td');
|
| 113 |
+
let rowData = [];
|
| 114 |
+
cells.forEach(cell => {
|
| 115 |
+
rowData.push('"' + cell.innerText.replace(/"/g, '""') + '"');
|
| 116 |
+
});
|
| 117 |
+
csv.push(rowData.join(','));
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
// Combine CSV rows
|
| 121 |
+
const csvContent = csv.join('\n');
|
| 122 |
+
|
| 123 |
+
// Create a Blob with the CSV content
|
| 124 |
+
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
|
| 125 |
+
|
| 126 |
+
// Create a download link
|
| 127 |
+
const link = document.createElement('a');
|
| 128 |
+
if (link.download !== undefined) {
|
| 129 |
+
const url = URL.createObjectURL(blob);
|
| 130 |
+
link.setAttribute('href', url);
|
| 131 |
+
link.setAttribute('download', 'table_data.csv');
|
| 132 |
+
link.style.visibility = 'hidden';
|
| 133 |
+
document.body.appendChild(link);
|
| 134 |
+
link.click();
|
| 135 |
+
document.body.removeChild(link);
|
| 136 |
+
}
|
| 137 |
}
|
| 138 |
+
|
| 139 |
+
// ... rest of the existing code ...
|
| 140 |
+
</script>
|
| 141 |
+
<script>
|
| 142 |
+
var req_tables = {{ req_tables | safe }};
|
| 143 |
+
</script>
|
| 144 |
+
<script>
|
| 145 |
+
document.addEventListener("DOMContentLoaded", function() {
|
| 146 |
+
console.log("Entered")
|
| 147 |
+
var dropdown = document.getElementById("table-dropdown");
|
| 148 |
+
console.log("The value is , ", dropdown.value)
|
| 149 |
+
var hiddenField = document.getElementById("hiddenField");
|
| 150 |
|
| 151 |
+
dropdown.addEventListener("change", function() {
|
| 152 |
+
hiddenField.value = dropdown.value;
|
| 153 |
+
});
|
| 154 |
+
});
|
| 155 |
+
</script>
|
| 156 |
{% endblock %}
|