-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.html
346 lines (286 loc) · 10.8 KB
/
test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="firebase.js"></script>
<script src="jquery-3.2.1.slim.min.js"></script>
<script src="bootstrap.min.js"></script>
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="fonts/font-awesome.min.css" >
</head>
<body onload="tampilData()">
<div style="padding:100px;">
<br>
<div class="row no-gutters">
<div class="col align-self-center"><h3 class="text-left"><i class="fa fa-home" onclick="tampilData()" style="cursor: pointer;"></i> Tabel Alat-Alat</h3></div>
<div class="col align-self-center" style="padding:12px;"><input id="text_cari" type="text" placeholder="Cari berdasarkan nama alat" class="form-control" style="padding:6px;" /></div>
<div class="col-auto align-self-center" style="padding:6px;"><button class="btn btn-primary" type="button" onclick="CariData()">Cari Data</button></div>
<div class="col-auto align-self-center" style="padding:6px;"><button class="btn btn-success" type="button" id="tambah_data" style="margin-left:10px;" data-toggle="modal" data-target="#ModalAdd" onclick="ambilDataTerakhir()">Tambah Data</button></div>
</div>
<table id="tabel-status-alat" class="table table-striped table-bordered table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Nama Alat</th>
<th scope="col">Status Alat</th>
<th scope="col">Tanggal Alat</th>
<th scope="col">Jam Alat</th>
<th scope="col">Aksi</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
// Initialize Firebase
var config = {
apiKey: ".....",
authDomain: ".....",
databaseURL: ".....",
projectId: ".....",
storageBucket: ".....",
messagingSenderId: "....."
};
firebase.initializeApp(config);
// Menampilkan data dalam bentuk tabel
function tampilData()
{
// Buat referensi database firebase
var dbRef = firebase.database();
var statusAlat = dbRef.ref("status-alat");
// Dapatkan referensi table
var table = document.getElementById("tabel-status-alat").getElementsByTagName('tbody')[0];
// Membuang semua isi table
$("#tabel-status-alat").find("tr:gt(0)").remove();
// Memuat Data
statusAlat.on("child_added", function(data, prevChildKey) {
var newstatusAlat = data.val();
var row = table.insertRow(table.rows.length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = newstatusAlat.id;
cell2.innerHTML = newstatusAlat.nama_alat;
cell3.innerHTML = newstatusAlat.status_alat;
cell4.innerHTML = newstatusAlat.tanggal_alat;
cell5.innerHTML = newstatusAlat.jam_alat;
cell6.innerHTML = '<button class="btn btn-primary btn-sm" type="button" id="update_data" onclick="updateData_Tampil(' + newstatusAlat.id + ')" data-toggle="modal" data-target="#ModalUpdate">Update</button><button class="btn btn-danger btn-sm" type="button" id="delete_data" onclick="deleteData_Tampil(' + newstatusAlat.id + ')" data-toggle="modal" data-target="#ModalDel" style="margin-left:10px;">Hapus</button>';
});
}
// Melakukan proses pencarian data
function CariData()
{
// Ambil isi text pencarian
var nama_alat_cari = $('#text_cari').val();
// Buat referensi database firebase
var dbRef = firebase.database();
var statusAlat = dbRef.ref("status-alat");
// Ambil data nama_alat sama persis isi text cari
// var query = statusAlat
// .orderByChild('nama_alat')
// .equalTo(nama_alat_cari)
// .limitToFirst(1);
// Ambil data nama_alat huruf depan (dan selebihnya) isi text cari dilimit 1 data saja
// var query = statusAlat
// .orderByChild('nama_alat')
// .startAt(nama_alat_cari)
// .endAt(nama_alat_cari + "\uf8ff")
// .limitToFirst(1);
// Ambil data nama_alat huruf depan (dan selebihnya) isi text cari
var query = statusAlat
.orderByChild('nama_alat')
.startAt(nama_alat_cari)
.endAt(nama_alat_cari + "\uf8ff");
// Dapatkan referensi table
var table = document.getElementById("tabel-status-alat").getElementsByTagName('tbody')[0];
// Membuang semua isi table
$("#tabel-status-alat").find("tr:gt(0)").remove();
// Memuat Data pencarian
query.on("child_added", function(snapshot) {
var childData = snapshot.val();
console.log(childData);
var row = table.insertRow(table.rows.length);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
var cell6 = row.insertCell(5);
cell1.innerHTML = childData.id;
cell2.innerHTML = childData.nama_alat;
cell3.innerHTML = childData.status_alat;
cell4.innerHTML = childData.tanggal_alat;
cell5.innerHTML = childData.jam_alat;
cell6.innerHTML = '<button class="btn btn-primary btn-sm" type="button" id="update_data" onclick="updateData_Tampil(' + childData.id + ')" data-toggle="modal" data-target="#ModalUpdate">Update</button><button class="btn btn-danger btn-sm" type="button" id="delete_data" onclick="deleteData_Tampil(' + childData.id + ')" style="margin-left:10px;"data-toggle="modal" data-target="#ModalDel">Hapus</button>';
});
}
// Menampilkan data yang akan di update kedalam modal update
function updateData_Tampil(id)
{
$('#T4').val(id);
var dbRef_update_tampil = firebase.database();
var statusAlatdenganID = dbRef_update_tampil.ref("status-alat/" + id);
statusAlatdenganID.on("value", function(snapshot) {
var childData = snapshot.val();
$('#t4_nama_alat').val(childData.nama_alat);
$('#t4_status_alat').val(childData.status_alat);
$('#t4_tanggal_alat').val(childData.tanggal_alat);
$('#t4_jam_alat').val(childData.jam_alat);
});
}
// Melakukan proses update data
function updateData_Proses()
{
var id_update_proses = $('#T4').val();
var nama_alat_update_proses = $('#t4_nama_alat').val();
var status_alat_update_proses = $('#t4_status_alat').val();
var tanggal_alat_update_proses = $('#t4_tanggal_alat').val();
var jam_alat_update_proses = $('#t4_jam_alat').val();
var dbRef_update_proses = firebase.database();
var update_statusAlat = dbRef_update_proses.ref("status-alat/" + id_update_proses);
update_statusAlat.update ({
"nama_alat": nama_alat_update_proses,
"status_alat": parseInt(status_alat_update_proses),
"tanggal_alat": tanggal_alat_update_proses,
"jam_alat": jam_alat_update_proses
});
$('#ModalUpdate').modal('hide');
tampilData();
}
// Mengambil id terakhir dan membahkan dengan 1 dan memasukkan kedalam text id di modal tambah
function ambilDataTerakhir()
{
$('#t4_nama_alat_add').val("");
$('#t4_status_alat_add').val("");
$('#t4_tanggal_alat_add').val("");
$('#t4_jam_alat_add').val("");
var dbRef_ambilDataTerakhir = firebase.database();
var cariAkhir = dbRef_ambilDataTerakhir.ref("status-alat");
cariAkhir.limitToLast(1).on('child_added', function(dataAkhir) {
var snap = dataAkhir.val();
var id_record_terakhir = snap.id + 1;
document.getElementById("T4_add").value = id_record_terakhir;
});
}
// Melakukan proses penambahan data
function addData_Proses()
{
var id_add_proses = $('#T4_add').val();
var nama_alat_add_proses = $('#t4_nama_alat_add').val();
var status_alat_add_proses = $('#t4_status_alat_add').val();
var tanggal_alat_add_proses = $('#t4_tanggal_alat_add').val();
var jam_alat_add_proses = $('#t4_jam_alat_add').val();
var dbRef_add_proses = firebase.database();
// Isikan data kedalam firebase
var statusAlat = dbRef_add_proses.ref("status-alat/" + id_add_proses);
statusAlat.set({
id : parseInt(id_add_proses),
jam_alat : jam_alat_add_proses,
nama_alat : nama_alat_add_proses,
status_alat : parseInt(status_alat_add_proses),
tanggal_alat : tanggal_alat_add_proses
});
$('#ModalAdd').modal('hide');
tampilData();
}
// Melakukan proses delete data yang telah dikonfirmasi sebelumnya
function delData_Proses()
{
var id_add_proses = $('#T4_del').val();
var dbRef_delete = firebase.database();
var statusAlat = dbRef_delete.ref("status-alat/" + id_add_proses);
statusAlat.remove();
$('#ModalDel').modal('hide');
tampilData();
}
// Memasukkan id ke textbox di modal konfirmasi delete
function deleteData_Tampil(id)
{
$('#T4_del').val(id);
}
</script>
<!-- Modal Update -->
<div class="modal fade" id="ModalUpdate" tabindex="-1" role="dialog" aria-labelledby="ModalUpdateLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ModalUpdateLabel">Update Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>ID : </h6>
<input class="form-control" type="text" id="T4">
<h6>Nama Alat : </h6>
<input class="form-control" type="text" id="t4_nama_alat">
<h6>Status Alat : </h6>
<input class="form-control" type="text" id="t4_status_alat">
<h6>Tanggal Alat : </h6>
<input class="form-control" type="text" id="t4_tanggal_alat">
<h6>Jam Alat : </h6>
<input class="form-control" type="text" id="t4_jam_alat">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
<button type="button" class="btn btn-primary" onclick="updateData_Proses()">Simpan</button>
</div>
</div>
</div>
</div>
<!-- Modal Add -->
<div class="modal fade" id="ModalAdd" tabindex="-1" role="dialog" aria-labelledby="ModalAddLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ModalUpdateLabel">Tambah Data Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>ID : </h6>
<input class="form-control" type="text" id="T4_add">
<h6>Nama Alat : </h6>
<input class="form-control" type="text" id="t4_nama_alat_add">
<h6>Status Alat : </h6>
<input class="form-control" type="text" id="t4_status_alat_add">
<h6>Tanggal Alat : </h6>
<input class="form-control" type="text" id="t4_tanggal_alat_add">
<h6>Jam Alat : </h6>
<input class="form-control" type="text" id="t4_jam_alat_add">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
<button type="button" class="btn btn-primary" onclick="addData_Proses()">Simpan</button>
</div>
</div>
</div>
</div>
<!-- Modal Hapus Data -->
<div class="modal fade" id="ModalDel" tabindex="-1" role="dialog" aria-labelledby="ModalDelLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ModalUpdateLabel">Konfirmasi Hapus Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>ID : </h6>
<input class="form-control" type="text" id="T4_del">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
<button type="button" class="btn btn-danger" onclick="delData_Proses()">Hapus Data</button>
</div>
</div>
</div>
</div>
</body>
</html>