-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (115 loc) · 3.66 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
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
let add = document.getElementById("add");
let edit = document.getElementById("edit");
let itemJsonArrayStr = localStorage.getItem("itemsJson");
let itemJsonArray = JSON.parse(itemJsonArrayStr);
let tableBody = document.getElementById("tableBody");
let str = "";
update(0);
function update(a1) {
if (a1 == 1) {
let itemJsonArray;
let tit = document.getElementById("Title").value;
let desc = document.getElementById("Description").value;
console.log("Updating List");
if (localStorage.getItem("itemsJson") == null) {
let itemJsonArray = [];
if(tit!=""&&desc!="")
itemJsonArray.push([tit, desc]);
localStorage.setItem("itemsJson", JSON.stringify(itemJsonArray));
} else {
let itemJsonArrayStr = localStorage.getItem("itemsJson");
itemJsonArray = JSON.parse(itemJsonArrayStr);
if(tit!=""&&desc!="")
itemJsonArray.push([tit, desc]);
localStorage.setItem("itemsJson", JSON.stringify(itemJsonArray));
}
tableBody = document.getElementById("tableBody");
str = "";
if (itemJsonArray != null) {
itemJsonArray.forEach((element, index) => {
str =
str +
`<tr id="trow">
<th scope="row">${index + 1}</th>
<td>${element[0]}</td>
<td>${element[1]}</td>
<td><button class="btn btn-primary btn-sm" id="edit" onclick="Edit(${index})" >Edit</button></td>
<td><button class="btn btn-primary btn-sm" id="edit" onclick="Delete(${index})">Delete</button></td>
</tr>`;
});
}
tableBody.innerHTML = str;
}
else {
let str = "";
if (itemJsonArray != null) {
itemJsonArray.forEach((element, index) => {
str =
str +
`<tr id="trow">
<th scope="row">${index + 1}</th>
<td>${element[0]}</td>
<td>${element[1]}</td>
<td><button class="btn btn-primary btn-sm" id="edit" onclick="Edit(${index})" >Edit</button></td>
<td><button class="btn btn-primary btn-sm" onclick="Delete(${index})">Delete</button></td>
</tr>`;
});
}
tableBody.innerHTML = str;
}
}
add.addEventListener("click", () => {
update(1);
});
function Delete(itemindex) {
console.log("Delete", itemindex + 1);
let itemJsonArrayStr = localStorage.getItem("itemsJson");
itemJsonArray = JSON.parse(itemJsonArrayStr);
//Delete itemindex from the array
itemJsonArray.splice(itemindex, 1);
localStorage.setItem("itemsJson", JSON.stringify(itemJsonArray));
update(0);
Swal.fire({
icon: 'success',
title: 'Successfully Deleted'
})
}
function Clear() {
if (confirm("Do you really want to clear ?")) {
console.log("clearing the storage");
localStorage.clear();
update(0);
}
}
//Sweat JS
add=document.getElementById("add");
add.addEventListener("click",()=>{
let tit = document.getElementById("Title").value;
let desc = document.getElementById("Description").value;
if(tit==""&&desc==""){
Swal.fire({
icon: 'error',
title: 'Please Enter Title and Decription'
})
}
else if(desc==""){
Swal.fire({
icon: 'error',
title: 'Please Enter Decription'
})
}
else if(tit==""){
Swal.fire({
icon: 'error',
title: 'Please Enter Title '
})
}
else{
Swal.fire({
icon: 'success',
title: 'Successfully Added'
})
document.getElementById("Title").value="";
document.getElementById("Description").value="";
}
})