có 1 mẹo làm thay nhưng mình đọc chưa hiểu lắm. Bạn nào giải thích rõ hơn tí dc ko ạ
- To input data from CSV files, use an html file with a table where your data is displayed on a browser tab, see below. Tag the table elements from that tab instead of reading from a file.
<html>
<head>
<style>
.table
{
width: 100%
}
.row .cell
{
padding: 5px;
border: 1px solid black;
}
</style>
<script>
window.onload = function () {
var datasource = document.getElementById('datasource');
datasource.addEventListener('change', function (e) {
var csvFile = datasource.files[0];
var fileReader = new FileReader();
fileReader.onloadend = function (e) {
var table = document.getElementById('table');
var lines = fileReader.result.split(/\r?\n/);
var n = 0;
lines.forEach(function (line) {
n++;
var tr = document.createElement("tr");
tr.setAttribute("class", "row");
tr.setAttribute("id", "row_" + n);
var cells = line.split(/,/);
var m = 0;
cells.forEach(function (cell) {
var td = document.createElement("td");
m++;
td.setAttribute("id", "cell_" + n + "-" + m);
td.setAttribute("class", "cell");
var text = document.createTextNode(cell);
td.appendChild(text);
tr.appendChild(td);
});
table.appendChild(tr);
});
}
fileReader.readAsText(csvFile);
}, false);
}
</script>
</head>
<body>
Select CSV file:
<input type="file" id="datasource" />
<div>Table with loaded CSV file:</div>
<table id="table" class=table></table>
</body>
</html>