-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
160 lines (151 loc) · 3.62 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Client WEB</title>
</head>
<body>
<div class="content-table">
<a href="javascript:;" onclick="showForm()">Tambah</a>
<table id="table_user" border="1" style="width: 100%;">
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>Alamat</th>
<th>Foto</th>
<th>Aksi</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="content-form" style="display: none;">
<form action="javascript:saveData('form-user');" name="form-user">
<table>
<tr>
<th>Nama</th>
<th>
<input type="hidden" name="user_id">
<input type="text" name="user_nama" required="">
</th>
</tr>
<tr>
<th>Alamat</th>
<th>
<input type="text" name="user_alamat" required="">
</th>
</tr>
<tr>
<th>Foto</th>
<th>
<input type="file"" onchange="getBase64Image(this)" required="">
<input type="hidden" name="user_photo">
</th>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Kirim">
</td>
</tr>
</table>
</form>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var BASE_URL = 'http://localhost:3000/'; // ganti sesuai dengan url
var BUCKET_NAME = 'https://clientbuckettia.s3.us-east-1.amazonaws.com/'; // ganti sesuai dengan bucket name
$(()=>{
loadTable();
})
function loadTable(){
$.ajax({
url: BASE_URL+'users',
type: "GET",
success: function(res){
$(`#table_user tbody`).html('');
var html = '';
if(res.length > 0){
$.each(res,(i,v)=>{
html += `
<tr>
<td>${(i+1)}</td>
<td>${v.user_nama}</td>
<td>${v.user_alamat}</td>
<td><img src="${BUCKET_NAME}${v.user_photo}" width="50px"></td>
<td><a href="javascript:;" onclick="updateData('${v.user_id}')">Ubah</a> | <a href="javascript:;" onclick="deleteData('${v.user_id}')">Hapus</a></td>
</tr>
`;
})
}else{
html = `
<tr>
<td colspan="5" align="center"> Data tidak ditemukan</td>
</tr>
`;
}
$(`#table_user tbody`).html(html);
}
})
}
function deleteData(id){
if(confirm('Yakin ingin menghapus data ini?')){
$.ajax({
url: BASE_URL+'deleteUsers/'+id,
type: 'POST',
success: function(res){
alert(res.message);
loadTable();
}
})
}
}
function showForm(reverse = false){
if(reverse){
$('.content-table').show();
$('.content-form').hide();
}else{
$('.content-table').hide();
$('.content-form').show();
}
}
function updateData(id){
showForm();
$.ajax({
url: BASE_URL+'users/'+id,
type: 'GET',
success: function(res) {
$.each(res[0],(i,v)=>{
$(`[name="${i}"]`).val(v);
})
$(`[name="user_photo"]`).val('');
}
});
}
function saveData(table){
var id = $('[name="user_id"]').val();
$.ajax({
url: BASE_URL+ (id ? 'updateUsers/'+id : 'insertUsers'),
type: "POST",
data: $(`[name="${table}"]`).serialize(),
success: function(res) {
alert(res.message);
$(`[name="${table}"]`).trigger('reset')
showForm(true);
loadTable();
}
})
}
function getBase64Image(el){
var file = el.files[0];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = function(event) {
var base64data = reader.result;
$(`[name="user_photo"]`).val(base64data);
}
}
</script>
</html>