-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (86 loc) · 2.82 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
85
86
87
88
89
90
91
92
93
94
function getAndUpdate(){
t = document.getElementById('task').value;
if (localStorage.getItem('stringitems')==null){
itemsarray = [];
itemsarray.push([t]);
localStorage.setItem('stringitems', JSON.stringify(itemsarray))
}
else{
stritemsarray = localStorage.getItem('stringitems')
itemsarray = JSON.parse(stritemsarray);
itemsarray.push([t]);
localStorage.setItem('stringitems', JSON.stringify(itemsarray))
}
update()
pendingtasks++;
updatetaskcount();
}
const tasks = document.getElementById("tablebody");
const completedcount = document.getElementById("completed-count");
const pendingcount = document.getElementById("pending-count");
let completedtasks = 0;
let pendingtasks = 0;
function updatetaskcount(){
completedcount.textContent = completedtasks;
pendingcount.textContent = pendingtasks;
}
function update(){
if (localStorage.getItem('stringitems')==null){
itemsarray = [];
localStorage.setItem('stringitems', JSON.stringify(itemsarray))
}
else{
stritemsarray = localStorage.getItem('stringitems')
itemsarray = JSON.parse(stritemsarray);
}
// Populate the table
let tablebody = document.getElementById("tablebody");
let str = "";
itemsarray.forEach((element, index) => {
str += `
<tr>
<th scope="r">${index + 1}</th>
<td>${element[0]}</td>
<td class="tbtns"><button onclick="donetask(${index})">Done</button><button onclick="deletetask(${index})">Delete</button></td>
</tr>`;
});
tablebody.innerHTML = str;
document.getElementById("task").value = "";
}
addtask = document.getElementById("addtask");
addtask.addEventListener("click", getAndUpdate);
update();
function donetask(taskindex){
// console.log("Delete", itemIndex);
stritemsarray = localStorage.getItem('stringitems')
itemsarray = JSON.parse(stritemsarray);
// Delete itemIndex element from the array
itemsarray.splice(taskindex, 1);
localStorage.setItem('stringitems', JSON.stringify(itemsarray));
update();
completedtasks++;
pendingtasks--;
updatetaskcount();
}
function deletetask(taskindex){
// console.log("Delete", itemIndex);
stritemsarray = localStorage.getItem('stringitems')
itemsarray = JSON.parse(stritemsarray);
// Delete itemIndex element from the array
itemsarray.splice(taskindex, 1);
localStorage.setItem('stringitems', JSON.stringify(itemsarray));
update();
pendingtasks--;
updatetaskcount();
}
function clearStorage(){
if (confirm("Do you areally want to clear?"))
{
localStorage.clear();
update()
}
document.getElementById("task").value = "";
completedtasks = 0;
pendingtasks = 0;
updatetaskcount();
}