binaychandra commited on
Commit
08f529a
·
1 Parent(s): b4abe72

modified download functionality

Browse files
Files changed (1) hide show
  1. templates/data.html +17 -39
templates/data.html CHANGED
@@ -93,51 +93,29 @@
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('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');
125
 
126
- // Create a Blob with the CSV content
127
- const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
128
-
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>
 
93
 
94
  // Function to handle the table data download
95
  function downloadTableData() {
96
+ const table = document.querySelector('.tabcontent:visible table');
97
+ if (table) {
98
+ const rows = table.querySelectorAll('tr');
99
+ let csvContent = '';
100
+
101
+ rows.forEach(row => {
102
+ const cols = row.querySelectorAll('td, th');
103
+ const rowData = Array.from(cols).map(col => col.innerText).join(',');
104
+ csvContent += rowData + '\n';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
  });
 
 
 
 
 
106
 
107
+ const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
108
+ const link = document.createElement('a');
109
+ link.href = URL.createObjectURL(blob);
110
+ link.download = 'table_data.csv';
111
+ link.style.display = 'none';
 
 
 
 
 
112
  document.body.appendChild(link);
113
  link.click();
114
  document.body.removeChild(link);
115
+ } else {
116
+ alert('No table data available to download.');
117
  }
118
+ }
119
 
120
  // ... rest of the existing code ...
121
  </script>