-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (58 loc) · 2.15 KB
/
index.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
function pushtoarray() {
title = document.getElementById('title').value;
desc = document.getElementById('description').value;
if (window.localStorage.getItem('itemsJson') == null) {
console.log('if loading........')
itemsJsonarray = [];
itemsJsonarray.push([title, desc]);
window.localStorage.setItem('itemsJson', JSON.stringify(itemsJsonarray));
}
else {
console.log('else loading....')
itemsJsonarraystr = window.localStorage.getItem('itemsJson');
itemsJsonarray = JSON.parse(itemsJsonarraystr);
itemsJsonarray.push([title, desc]);
window.localStorage.setItem('itemsJson', JSON.stringify(itemsJsonarray));
}
constructtable();
}
function constructtable() {
if (window.localStorage.getItem('itemsJson')==null){
itemsJsonarray = [];
window.localStorage.setItem('itemsJson', JSON.stringify(itemsJsonarray))
}
else{
itemsJsonarraystr = window.localStorage.getItem('itemsJson')
itemsJsonarray = JSON.parse(itemsJsonarraystr);
}
let tablebody = document.getElementById('tablebody');
let str = "";
if (itemsJsonarraystr != '[]') {
str += `<tr>
<th>S No.</th>
<th>Title</th>
<th>Description</th>
<th>Actions</th>
</tr>`
}
itemsJsonarray.forEach((element, index) => {
str += `
<tr>
<th scope="row">${index + 1}</th>
<td>${element[0]}</td>
<td>${element[1]}</td>
<td><button class='tablebutton' onclick=" del(${index})">Delete</button></td>
</tr>
` ;
});
tablebody.innerHTML = str;
}
constructtable();
submit.addEventListener("click", pushtoarray);
function del(itemindex) {
itemsJsonarraystr = window.localStorage.getItem('itemsJson');
itemsJsonarray = JSON.parse(itemsJsonarraystr);
itemsJsonarray.splice(itemindex, 1);
window.localStorage.setItem('itemsJson', JSON.stringify(itemsJsonarray));
constructtable();
}