-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVExplorer.html
88 lines (75 loc) · 2.37 KB
/
CSVExplorer.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<html>
<head>
<meta charset="ISO-8859-1">
<title>CSV to HTML table rendering</title>
<link rel='stylesheet' type="text/css" href='https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css' />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
var dataSet;
var ISSUE_FILE_HEADER = ['First name', 'Sur name', 'Issue count', 'Date of birth' ];
//initTestData();
function initTestData() {
/*dataSet = [
[ "Jayaprabahar", "Chandrasekaran", "4", "2017/03/12" ]
];*/
}
function fileToText(file, callback) {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
callback(reader.result);
};
}
function refreshDatatable(uploadData) {
$('#example').dataTable().fnClearTable();
var perRowArray = uploadData.split(/\r?\n/);
perRowArray.forEach(function(element) {
element = element.replace(/['"]+/g, '');
var splittedString = element.split(",");
var isHeaderString = false;
for (i = 0; i < splittedString.length; i++) {
if(ISSUE_FILE_HEADER.includes(splittedString[i])) {
isHeaderString = true;
}
}
if(!isHeaderString)
$('#example').dataTable().fnAddData(splittedString);
});
}
$(document).ready(function() {
const input = document.querySelector("#input");
input.addEventListener("change", () => {
const file = input.files.item(0);
fileToText(file, (text) => {
refreshDatatable(text);
});
});
$('#example').DataTable( {
data: dataSet,
columns: [
{ title: ISSUE_FILE_HEADER[0] },
{ title: ISSUE_FILE_HEADER[1] },
{ title: ISSUE_FILE_HEADER[2] },
{ title: ISSUE_FILE_HEADER[3] }
]
} );
} );
</script>
</head>
<body>
<div>
Please upload CSV file:-
<input id="input" type="file" name="bankfile" accept=".csv">
</div>
<br/>
<table id="example" class="display" style="width:100%">
</table>
</body>
</html>