-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
158 lines (132 loc) · 3.8 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<style>
#jsonArea{
width:100%;
height:100px;
}
#jsonURL {
width:100%;
}
table{
border-collapse : collapse;
}
</style>
<h2>JSON To Table</h2>
<textArea id="jsonArea" placeholder="Enter Valid JSON here..."></textArea>
<input type="text" id="jsonURL" placeholder="Enter Valid JSON URL here..." hidden="true"/>
<br/>
<button onclick="parse()">
Create Table
</button>
<button id="download" hidden="true">
Download
</button>
<br/>
<br/>
<table id="jsonTable" style="border : 1px"/>
<script>
function parse(){
var jsonArea = document.getElementById('jsonArea').value;
var jsonURL = document.getElementById('jsonURL').value;
var table = document.getElementById('jsonTable');
var download = document.getElementById("download");
download.hidden= true;
table.innerHTML = "";
if((jsonArea != undefined && jsonArea != null && jsonArea.trim() != "") && (jsonURL != undefined && jsonURL != null && jsonURL.trim() != "")){
alert('Please provide either JSON OR URL as input.');
}
else if(jsonArea != undefined && jsonArea != null && jsonArea.trim() != ""){
formTable(jsonArea);
}
else if(jsonURL != undefined && jsonURL != null && jsonURL.trim() != ""){
fetch(jsonURL)
.then(json => {
formTable(json);
}).catch(error=>{
alert('Error while fetch from URL : '+error);
});
}else{
alert('Please provide either JSON OR URL as input.');
}
}
function isValidJson(json){
console.log(json);
return true;
}
function formTable(json){
if(!isValidJson(json)){
alert('No Valid Json found.');
return;
}
var table = document.getElementById('jsonTable');
var jsonStr = JSON.parse(json);
var setHeaders = true;
for(i in jsonStr){
var item = JSON.stringify(jsonStr[i]);
if(setHeaders){
createEntry(table, item, true);
setHeaders = false;
}
createEntry(table,item, false);
}
formatTable(table);
var download = document.getElementById("download");
download.hidden= false;
}
function createEntry(table,item,isHeader){
var tr = document.createElement("tr");
var json = JSON.parse(item);
for(i in json){
console.log(i);
var td = document.createElement("td");
if(isHeader){
var bold = document.createElement('b');
var textnode = document.createTextNode(i);
bold.appendChild(textnode);
td.appendChild(bold);
}else{
var textnode = document.createTextNode(json[i]);
td.appendChild(textnode);
}
formatTable(td);
tr.appendChild(td);
}
table.appendChild(tr);
}
function formatTable(item){
item.style.border = '1px Solid black';
}
function download_csv(csv, filename) {
var csvFile;
var downloadLink;
// CSV FILE
csvFile = new Blob([csv], {type: "text/csv"});
// Download link
downloadLink = document.createElement("a");
// File name
downloadLink.download = filename;
// We have to create a link to the file
downloadLink.href = window.URL.createObjectURL(csvFile);
// Make sure that the link is not displayed
downloadLink.style.display = "none";
// Add the link to your DOM
document.body.appendChild(downloadLink);
// Lanzamos
downloadLink.click();
}
function export_table_to_csv(html, filename) {
var csv = [];
var rows = document.querySelectorAll("table tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++)
row.push(cols[j].innerText);
csv.push(row.join(","));
}
// Download CSV
download_csv(csv.join("\n"), filename);
}
document.getElementById("download").addEventListener("click", function () {
var html = document.querySelector("table").outerHTML;
export_table_to_csv(html, "table.csv");
});
</script>