-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
97 lines (76 loc) · 2.1 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
const modal = document.querySelector('.modal-container')
const tbody = document.querySelector('tbody')
const sNome = document.querySelector('#m-nome')
const sFuncao = document.querySelector('#m-funcao')
const sSalario = document.querySelector('#m-salario')
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) {
sNome.value = itens[index].nome
sFuncao.value = itens[index].funcao
sSalario.value = itens[index].salario
id = index
} else {
sNome.value = ''
sFuncao.value = ''
sSalario.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.nome}</td>
<td>${item.funcao}</td>
<td>R$ ${item.salario}</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 (sNome.value == '' || sFuncao.value == '' || sSalario.value == '') {
return;
}
e.preventDefault();
if (id !== undefined) {
itens[id].nome = sNome.value
itens[id].funcao = sFuncao.value
itens[id].salario = sSalario.value
} else {
itens.push({'nome': sNome.value, 'funcao': sFuncao.value, 'salario': sSalario.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();