-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
185 lines (145 loc) · 4.46 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var form = document.getElementById('submit-form');
var items= document.getElementById('items');
var description = document.getElementById('description');
var title = document.getElementById('title');
var description = document.getElementById('description');
var tablediv = document.getElementById('tablediv');
var addrecords = document.getElementById('add-records');
var search = document.getElementById('search');
var resetbtn = document.getElementById('resetbtn');
// number of notes
var notecount = 0;
// append new notes
var newnote ='';
//View and update
isUpdate= false;
note = '';
record = '';
body = '';
// load view
window.onload = updateTable();
// view and update notes
items.addEventListener('click', viewNotes);
// delete notes
items.addEventListener('click', removeNotes);
// Search notes
search.addEventListener('keyup', searchNote);
// Add notes
form.addEventListener('submit', addNote);
// reset text fields
resetbtn.addEventListener('click', resetAll);
// table visibility
function updateTable(){
if(notecount > 0){
tablediv.style.display='';
if(isUpdate){
note.firstChild.textContent = title.value;
note.lastChild.textContent = description.value;
isUpdate = false;
notecount--;
}
else{
items.appendChild(newnote);
}
}
else{
tablediv.style.display = 'none';
addrecords.style.margin= '0 auto';
}
}
// Add new notes
function addNote(e){
e.preventDefault();
//new table row
var tr= document.createElement('tr');
// add a class name for use for searchings
tr.className = 'item';
// table data rows
var td1 = document.createElement('td');
td1.appendChild(document.createTextNode(notecount+1));
var td2 = document.createElement('td');
td2.className='item-section';
td2.appendChild(document.createTextNode(title.value));
var span = document.createElement('span');
span.className='note-body';
span.appendChild(document.createTextNode(description.value));
td2.appendChild(span);
var td3 = document.createElement('td');
td3.className= 'btn-view';
var btn1 = document.createElement('button');
btn1.appendChild(document.createTextNode('View'));
btn1.setAttribute('id','viewbtn');
td3.appendChild(btn1);
var td4 = document.createElement('td');
td4.className= 'btn-delete';
var btn2 = document.createElement('button');
btn2.appendChild(document.createTextNode('Delete'));
btn2.setAttribute('id','deletebtn');
td4.appendChild(btn2);
// append all data rows to table row
tr.appendChild(td1);
tr.appendChild(td2);
tr.appendChild(td3);
tr.appendChild(td4);
notecount++;
newnote =tr;
// append new notes to newnote variable
updateTable();
//reset all fields after update or add note
resetAll();
}
//search note option
function searchNote(e){
// convert to lowercase
var searchText = e.target.value.toLowerCase();
//get the all table items
var list = items.getElementsByClassName('item');
// convert table items into a array
var listArray = Array.from(list);
// check each array item
listArray.forEach(function (itm){
// get the title text from the above array
var titletext = itm.children[1].textContent;
// matching the title text vs searching text
if(titletext.toLowerCase().indexOf(searchText) != -1){
itm.style.display ='';
}
else{
itm.style.display = 'none';
}
});
}
// Remove notes from table
function removeNotes(e){
var removeitem = e.target.id;
if(removeitem === 'deletebtn'){
if(confirm('Are you sure?')){
//select the whole row to delete
var row = e.target.parentElement.parentElement;
// delete the row
items.removeChild(row);
notecount--;
if(notecount < 1){
updateTable();
}
}
}
}
// view and update notes
function viewNotes(e){
var viewitem = e.target.id;
if(viewitem === 'viewbtn'){
record = e.target.parentElement.parentElement;
note = record.children[1];
title.value=note.firstChild.textContent;
description.value = note.lastChild.textContent;
isUpdate = true;
}
}
// reset all text fields
function resetAll(){
title.value = '';
description.value = '';
isUpdate = false;
newnote = '';
}