-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (69 loc) · 2.42 KB
/
script.js
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
const submit = document.getElementById('submit')
const tbody = document.getElementById('table-body');
var obj = [];
submit.addEventListener('click', (e) => {
e.preventDefault();
const form = document.getElementById('form');
const formData = new FormData(form);
var jsonData = {};
let empty = false;
for (const [key, value] of formData) {
if (value === "") {
empty = true;
}
else {
jsonData[key] = value;
}
}
if (!empty) {
obj.push(jsonData)
tbody.innerHTML += `<tr id=${formData.get('id')}>
<td>${formData.get('id')}</td>
<td>${formData.get('name')}</td>
<td>${formData.get('phone')}</td>
<td>${formData.get('email')}</td>
<td><span class="material-symbols-outlined deleteRow">
delete
</span></td>
</tr>`
document.getElementById('json-data').innerHTML = syntaxHighlight(obj)
form.reset();
document.getElementById('id').value = Number(formData.get('id')) + 1;
}
// Delete Row
const del = document.querySelectorAll('.deleteRow');
del.forEach(row => {
row.addEventListener('click', (e) => {
const rowId = e.target.parentNode.parentNode.id
console.log(rowId);
delete obj[rowId - 1]
document.getElementById('json-data').innerHTML = syntaxHighlight(obj)
document.getElementById(rowId).innerHTML = "";
});
});
})
// To pretty Javascript
function syntaxHighlight(obj) {
if (typeof obj != 'string') {
obj = JSON.stringify(obj, null, 2);
}
obj = obj.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return obj.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?,)/g, function (match) {
var cls = 'token number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'token string-property property';
} else {
cls = 'token string';
}
} else if (/true|false/.test(match)) {
cls = 'token boolean';
} else if (/null/.test(match)) {
cls = 'token null';
} else if (/0-9/.test(match)) {
cls = 'token number';
}
return '<span class="' + cls + '">' + match + '</span>';
// return match;
})
}