-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalunos.html
186 lines (159 loc) · 3.62 KB
/
alunos.html
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
186
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Alunos</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script src="https://unpkg.com/@supabase/supabase-js@2"></script>
<script src="clientsupabase.js"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
#tbalunos {
width: 100%;
}
#tbalunos td {
border: solid 1px #3832e3;
}
#tbalunos td:nth-child(1) {
text-align: center;
}
#tbalunos td:nth-child(2) {
width: 15%;
text-align: center;
}
#tbalunos td:nth-child(3) {
width: 45%;
}
#tbalunos td:nth-child(4) {
width: 15%;
text-align: center;
}
#tbalunos td:nth-child(5) {
width: 15%;
text-align: center;
}
.link {
cursor: pointer;
}
.link:hover {
color: blue;
}
</style>
</head>
<body>
<div id="menu"></div>
<fieldset>
<legend>Aluno</legend>
<input type="hidden" id="hId" />
<input type="text" id="txnome" />
<input type="text" id="txmatricula" />
<button onclick="saveAluno()">Salvar</button>
</fieldset>
<table id="tbalunos">
<thead>
<tr>
<th>Id</th>
<th onclick="setOrder()" class="link">Data</th>
<th>Descrição</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<script>
/*carrega menu*/
$("#menu").load("menu.html");
/*dados da app*/
var alunos = [];
var order = true;
const setOrder = async () => {
order = !order;
await getAlunos();
}
/*métodos básicos*/
var client = ClientSubapase.getClient();
const getAlunos = async function () {
const { data, error } = await client
.from('alunos')
.select()
.order('nome', { ascending: order });
alunos = data;
tableBody = $("#tbalunos tbody").empty();
alunos.forEach((t) => {
tableBody = $("#tbalunos tbody").append(`<tr>
<td>${t.id}</td>
<td>${t.nome}</td>
<td>${t.matricula}</td>
<td><button onclick='selAluno(${t.id})'>Selecionar</button></td>
<td><button onclick='deleteAluno(${t.id})'>Excluir</button></td>
</tr>`);
});
$('#hId').val('');
$('#txnome').val('');
$('#txmatricula').val('');
}
const saveAluno = async function () {
if ($('#hId').val() != '') {
await updateAluno();
} else {
const { error } = await client
.from('alunos')
.insert({
matricula: $('#txmatricula').val(),
nome: $('#txnome').val()
});
if (error != null) {
alert('digite os campos')
}
await getAlunos();
}
}
const deleteAluno = async function (_id) {
if (confirm('Confirma a exclusão?')) {
const response = await client
.from('alunos')
.delete()
.eq('id', _id)
if (response != null && response.error != null) {
alert(response.error.message);
}
await getAlunos();
}
}
const selAluno = async function (_id) {
const { data, error } = await client
.from('alunos')
.select()
.eq('id', _id);
console.log(data);
$('#txmatricula').val(data[0].matricula);
$('#txnome').val(data[0].nome);
$('#hId').val(data[0].id);
}
const updateAluno = async function () {
const { error } = await client
.from('alunos')
.update({
matricula: $('#txmatricula').val(),
nome: $('#txnome').val()
})
.eq('id', $('#hId').val());
if (error != null) {
alert('digite os campos')
}
await getAlunos();
}
/*método que inicia a aplicação*/
async function startApp() {
let u = await ClientSubapase.getUser();
if (u == null) {
window.location = "login.html";
}
await getAlunos();
}
startApp();
</script>
</body>
</html>