New programmer here. I am trying to replicate a youtube video for a program that creates a CSV table viewer on html and am getting this error SyntaxError: The requested module './TableCsv.js' does not provide an export named 'default'
I have tried adding curly braces around TableCsv in main.js but no luck. When I try adding my own export in TableCsv.js it says A module cannot have multiple default exports.ts(2528).
Here is my code
main.js
import TableCsv from "./TableCsv.js";
const tableRoot = document.querySelector("#csvRoot");
const csvFileInput = document.querySelector("#csvFileInput");
const tableCsv = new TableCsv(tableRoot);
csvFileInput.addEventListener("change", e => {
Papa.parse(csvFileInput.files[0], {
delimiter: ",",
skipEmptyLines: true,
complete: results => {
tableCsv.update(results.data.slice(1), results.data[0]);
}
});
});
TableCsv.js
class TableCsv {
/**
* @param {HTMLTableElement} root The table element which will display the CSV data.
*/
constructor(root) {
this.root = root;
}
/**
* Clears existing data in the table and replaces it with new data.
*
* @param {string[][]} data A 2D array of data to be used as the table body
* @param {string[]} headerColumns List of headings to be used
*/
update(data, headerColumns = []) {
this.clear();
this.setHeader(headerColumns);
this.setBody(data);
}
/**
* Clears all contents of the table (incl. the header).
*/
clear() {
this.root.innerHTML = "";
}
/**
* Sets the table header.
*
* @param {string[]} headerColumns List of headings to be used
*/
setHeader(headerColumns) {
this.root.insertAdjacentHTML(
"afterbegin",
`
<thead>
<tr>
${headerColumns.map((text) => `<th>${text}</th>`).join("")}
</tr>
</thead>
`
);
}
/**
* Sets the table body.
*
* @param {string[][]} data A 2D array of data to be used as the table body
*/
setBody(data) {
const rowsHtml = data.map((row) => {
return `
<tr>
${row.map((text) => `<td>${text}</td>`).join("")}
</tr>
`;
});
this.root.insertAdjacentHTML(
"beforeend",
`
<tbody>
${rowsHtml.join("")}
</tbody>
`
);
}
}
const tableRoot = document.querySelector("#csvRoot");
const csvFileInput = document.querySelector("#csvFileInput");
const tableCsv = new TableCsv(tableRoot);
csvFileInput.addEventListener("change", (e) => {
Papa.parse(csvFileInput.files[0], {
delimiter: ",",
skipEmptyLines: true,
complete: (results) => {
tableCsv.update(results.data.slice(1), results.data[0]);
}
});
});