-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.js
56 lines (50 loc) · 1.59 KB
/
list.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
let data = [];
axios
.get("http://localhost:3000/Buku")
.then((res) => {
const listsHTML = document.querySelector("#listdata");
data = res.data;
data.forEach((item) => {
const { id, namaBuku, penerbitBuku, jumlahHalaman, jumlahBuku } = item;
const tableHTML = `<tr>
<td>${namaBuku}</td>
<td>${penerbitBuku}</td>
<td>${jumlahHalaman}</td>
<td>${jumlahBuku}</td>
<td>
<button type="button" onclick="update(${id})" class="btn btn-outline-secondary">
<i class="far fa-edit"></i>
</button>
<button type="button" onclick="remove(${id})" class="btn btn-outline-danger">
<i class="fas fa-trash-alt"></i>
</button>
</td>
</tr>
`;
listsHTML.innerHTML += tableHTML;
});
})
.catch((pesanError) => {
console.error(pesanError);
});
const update = (id) => {
const listData = data.find((item) => {
return item.id === id;
});
if (listData) {
const namaBuku = window.prompt("Nama Buku", listData.namaBuku);
const penerbitBuku = window.prompt("Penerbit Buku", listData.penerbitBuku);
const jumlahHalaman = window.prompt("Jumlah Halaman", listData.jumlahHalaman);
const jumlahBuku = window.prompt("Jumlah Buku", listData.jumlahBuku);
const data = {
namaBuku,
penerbitBuku,
jumlahHalaman,
jumlahBuku,
};
axios.put(`http://localhost:3000/Buku/${id}`, data);
}
};
const remove = (id) => {
axios.delete(`http://localhost:3000/Buku/${id}`);
};