-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvtable.js
40 lines (33 loc) · 1.18 KB
/
csvtable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(function(global, $) {
'use strict';
function generateTableFromCSV(csvPath) {
$.ajax({
url: csvPath,
dataType: 'text',
}).done(function(data) {
// Parse the CSV file
const parsedData = Papa.parse(data, {
header: true, // Assumes the first row contains the headers
dynamicTyping: true,
});
// Create HTML table
let tableHtml = '<thead><tr>';
for (const header of parsedData.meta.fields) {
tableHtml += `<th>${header}</th>`;
}
tableHtml += '</tr></thead><tbody>';
for (const row of parsedData.data) {
tableHtml += '<tr>';
for (const header of parsedData.meta.fields) {
tableHtml += `<td>${row[header]}</td>`;
}
tableHtml += '</tr>';
}
tableHtml += '</tbody>';
$('#csvTable').html(tableHtml);
new DataTable($('#csvTable'));
});
}
// Expose the function to the global scope
global.generateTableFromCSV = generateTableFromCSV;
}(window, jQuery));