binaychandra commited on
Commit
b4abe72
·
1 Parent(s): 152735e

fixing bug for downloading

Browse files
Files changed (1) hide show
  1. 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
- 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');
@@ -126,15 +129,15 @@
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>
 
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>