2018-10-21 10:18:16 +00:00
|
|
|
// Source: https://blog.mounirmesselmeni.de/2012/11/20/reading-csv-file-with-javascript-and-html5-file-api/
|
|
|
|
// Source: https://www.js-tutorials.com/javascript-tutorial/reading-csv-file-using-javascript-html5/
|
2018-10-22 08:17:57 +00:00
|
|
|
// Source: https://stackoverflow.com/questions/13329853/reading-server-file-with-javascript
|
2018-10-21 10:18:16 +00:00
|
|
|
|
2018-10-22 08:17:57 +00:00
|
|
|
function handleFile() {
|
2018-10-21 10:18:16 +00:00
|
|
|
// Check for the various File API support.
|
|
|
|
if (window.FileReader) {
|
|
|
|
// FileReader are supported.
|
2018-10-22 08:17:57 +00:00
|
|
|
var xmlhttp = new XMLHttpRequest();
|
|
|
|
xmlhttp.onreadystatechange = function () {
|
|
|
|
if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
|
|
|
|
processData(xmlhttp.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xmlhttp.open("GET", "world_data_v1.csv", true);
|
|
|
|
xmlhttp.send();
|
2018-10-21 10:18:16 +00:00
|
|
|
} else {
|
|
|
|
alert('FileReader are not supported in this browser.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processData(csv) {
|
2018-10-22 08:29:01 +00:00
|
|
|
var table = "<table style='width:100%'>";
|
|
|
|
|
|
|
|
// split rows
|
2018-10-21 10:18:16 +00:00
|
|
|
var allTextLines = csv.split(/\r\n|\n/);
|
|
|
|
|
2018-10-22 08:29:01 +00:00
|
|
|
// create table header
|
|
|
|
table += "<tr>";
|
|
|
|
|
|
|
|
var data = allTextLines[0].split(",");
|
|
|
|
|
|
|
|
for (var j = 0; j < data.length; j++) {
|
|
|
|
table += "<th>";
|
|
|
|
table += data[j];
|
|
|
|
table += "</th>";
|
|
|
|
}
|
|
|
|
|
|
|
|
table += "</tr>";
|
|
|
|
|
|
|
|
// create table content
|
|
|
|
for (var i = 1; i < allTextLines.length; i++) {
|
|
|
|
var data = allTextLines[i].split(',');
|
2018-10-21 10:18:16 +00:00
|
|
|
|
|
|
|
table += "<tr>";
|
|
|
|
|
|
|
|
for (var j = 0; j < data.length; j++) {
|
|
|
|
table += "<td>";
|
|
|
|
table += data[j];
|
2018-10-22 08:29:01 +00:00
|
|
|
table += "</td>";
|
2018-10-21 10:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
table += "</tr>";
|
|
|
|
}
|
|
|
|
|
|
|
|
table += "</table>";
|
2018-10-21 16:03:11 +00:00
|
|
|
|
2018-10-22 08:29:01 +00:00
|
|
|
document.getElementById('t01').innerHTML = table;
|
2018-10-21 10:18:16 +00:00
|
|
|
}
|