-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathmain.js
More file actions
66 lines (66 loc) · 2.29 KB
/
Copy pathmain.js
File metadata and controls
66 lines (66 loc) · 2.29 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$(document).ready(function () {
function checkCSVFile(name) {
return (name.slice(name.length - 4, name.length) === '.csv')
}
function renderCSVtoTable(data) {
var csv_data = data.split(/\r?\n|\r/);
var table_data = '<table class="table table-bordered table-striped">';
for (let row_count = 0; row_count < csv_data.length; row_count++) {
const cell_data = csv_data[row_count].split(',');
table_data += '<tr>';
for (let cell_count = 0; cell_count < cell_data.length; cell_count++) {
const element = cell_data[cell_count];
if (cell_count === 0) {
table_data += '<th>' + element + '</th>';
} else {
table_data += '<th>' + element + '</th>';
}
}
table_data += '</tr>';
}
table_data += '</table>';
return table_data;
}
function loadHandler(event) {
var csv = event.target.result;
$('#data_table').html(renderCSVtoTable(csv));;
}
function errorHandler(evt) {
if (evt.target.error.name == "NotReadableError") {
alert("Canno't read file !");
}
}
function getAsText(fileToRead) {
var reader = new FileReader();
// Read file into memory as UTF-8
reader.readAsText(fileToRead);
// Handle errors load
reader.onload = loadHandler;
reader.onerror = errorHandler;
}
function handleFiles(files) {
// Check for the various File API support.
if (window.FileReader) {
// FileReader are supported.
getAsText(files[0]);
} else {
alert('FileReader are not supported in this browser.');
}
}
// detect a change in a file input with an id of “the-file-input”
$("#csv-upload").change(function () {
// will log a FileList object, view gifs below
if(checkCSVFile(this.files[0].name)) {
$('#data_table').html(handleFiles(this.files));
}
});
$('#load_data').click(function () {
$.ajax({
url: "train.csv",
dataType: "text",
success: function (data) {
$('#data_table').html(renderCSVtoTable(data));
}
})
})
})