-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
140 lines (113 loc) · 4.52 KB
/
app.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
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
document.addEventListener('DOMContentLoaded', function () {
// Initialize DataTable
$('#example').DataTable({
scrollY: '600px',
scrollCollapse: true,
paging: true
});
let getData = localStorage.getItem('customers') ? JSON.parse(localStorage.getItem('customers')) : []
let isEdit = false, editId
showInfo()
var form = document.getElementById('addNewForm')
var customer_Name = document.getElementById('customerName')
var customer_Address = document.getElementById('customerAddress')
var customerEmail = document.getElementById('email')
var phoneNumber = document.getElementById('phone')
var productName = document.getElementById('product')
var deliveryDate = document.getElementById('deliveryDate')
var addNewModal
// Show modal when "Add New" button is clicked
document.getElementById('addNewBtn').addEventListener('click', function () {
addNewModal = new bootstrap.Modal(document.getElementById('addNewModal'), {
backdrop: 'static',
keyboard: false
});
addNewModal.show();
document.querySelector('.submitBtn').innerText = 'Submit'
document.querySelector('.modal-title').inneText = "Add New Customer"
isEdit = false
document.getElementById('addNewForm').reset()
});
function showInfo(){
// Get the DataTable instance
let table = $('#example').DataTable();
table.clear(); // Clear existing data in the DataTable
getData.forEach((element, index) => {
// Add the row to the DataTable
table.row.add([
index + 1,
element.customerName,
element.customerAddress,
element.email,
element.phone,
element.productName,
element.deliveryDate,
`<td class="actionIcon">
<button class="actionBtn" onclick="editInfo(${index}, '${element.customerName}', '${element.customerAddress}', '${element.email}', '${element.phone}', '${element.productName}', '${element.deliveryDate}')">Edit</button>
<button class="actionBtn" onclick="deleteInfo(${index})">Delete</button>
</td>`
]);
})
// Redraw the DataTable to display the new rows
table.draw();
}
showInfo()
window.editInfo = function(index, name, address, email, phone, product, deDate) {
isEdit = true
editId = index
customer_Name.value = name
customer_Address.value = address
customerEmail.value = email
phoneNumber.value = phone
productName.value = product
deliveryDate.value = deDate
document.querySelector('.submitBtn').innerText = 'Update'
document.querySelector('.modal-title').innerText = "Update The Form"
addNewModal = new bootstrap.Modal(document.getElementById('addNewModal'), {
backdrop: 'static',
keyboard: false
});
addNewModal.show();
document.querySelector('.submitBtn').innerText = 'Update'
document.querySelector('.modal-title').innerText = "Update The Form"
}
window.deleteInfo = function(index) {
if (confirm("Are you sure you want to delete this customer?")) {
getData.splice(index, 1);
localStorage.setItem('customers', JSON.stringify(getData));
showInfo();
}
}
form.addEventListener('submit', (e)=> {
e.preventDefault()
const information = {
customerName: customer_Name.value,
customerAddress: customer_Address.value,
email: customerEmail.value,
phone: phoneNumber.value,
productName: productName.value,
deliveryDate: deliveryDate.value,
}
if(!isEdit){
getData.push(information)
}
else{
isEdit = false
getData[editId] = information
}
localStorage.setItem('customers', JSON.stringify(getData))
document.querySelector('.submitBtn').innerText = 'Submit'
document.querySelector('.modal-title').inneText = "Add New Customer"
showInfo()
// Close the modal after submission
addNewModal.hide();
form.reset()
})
});
// Mendapatkan elemen tombol logout
const logoutButton = document.getElementById('logoutButton');
// Menangani klik tombol logout
logoutButton.addEventListener('click', function() {
// Redirect ke halaman login
window.location.href = 'login.html';
});