-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
96 lines (76 loc) · 2.04 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
const modal = document.querySelector('.modal-container')
const tbody = document.querySelector('tbody')
const sName = document.querySelector('#m-name')
const sEmail = document.querySelector('#m-email')
const sNasc = document.querySelector('#m-nasc')
const btnSalvar = document.querySelector('#btnSalvar')
let itens
let id
function openModal(edit = false, index = 0) {
modal.classList.add('active')
modal.onclick = e => {
if (e.target.className.indexOf('modal-container') !== -1) {
modal.classList.remove('active')
}
}
if (edit) {
sName.value = itens[index].name
sEmail.value = itens[index].email
sNasc.value = itens[index].nasc
id = index
} else {
sName.value = ''
sEmail.value = ''
sNasc.value = ''
}
}
function editItem(index) {
openModal(true, index)
}
function deleteItem(index) {
itens.splice(index, 1)
setItensBD()
loadItens()
}
function insertItem(item, index) {
let tr = document.createElement('tr')
tr.innerHTML = `
<td>${item.name}</td>
<td>${item.email}</td>
<td>${item.nasc}</td>
<td class="acao">
<button onclick="editItem(${index})"><i class='bx bx-edit' ></i></button>
</td>
<td class="acao">
<button onclick="deleteItem(${index})"><i class='bx bx-trash'></i></button>
</td>
`
tbody.appendChild(tr)
}
btnSalvar.onclick = e => {
if (sName.value == '' || sEmail.value == '' || sNasc.value == '') {
return
}
e.preventDefault();
if (id !== undefined) {
itens[id].name = sName.value
itens[id].email = sEmail.value
itens[id].nasc = sNasc.value
} else {
itens.push({ 'name': sName.value, 'email': sEmail.value, 'nasc': sNasc.value })
}
setItensBD()
modal.classList.remove('active')
loadItens()
id = undefined
}
function loadItens() {
itens = getItensBD()
tbody.innerHTML = ''
itens.forEach((item, index) => {
insertItem(item, index)
})
}
const getItensBD = () => JSON.parse(localStorage.getItem('dbfunc')) ?? []
const setItensBD = () => localStorage.setItem('dbfunc', JSON.stringify(itens))
loadItens()