-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (67 loc) · 3 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
document.addEventListener('DOMContentLoaded', function () {
const form = document.getElementById('manufacturing-form');
const tableBody = document.querySelector('#manufacturing-table tbody');
let records = JSON.parse(localStorage.getItem('manufacturingRecords')) || [];
// Function to render records to the table
function renderTable() {
tableBody.innerHTML = '';
records.forEach((record, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${record.productName}</td>
<td>${record.quantity}</td>
<td>${record.date}</td>
<td>${record.status}</td>
<td class="actions">
<button class="edit-btn" data-index="${index}">Edit</button>
<button class="delete-btn" data-index="${index}">Delete</button>
</td>
`;
tableBody.appendChild(row);
});
}
// Function to add a new record
function addRecord(event) {
event.preventDefault();
const productName = document.getElementById('product-name').value;
const quantity = document.getElementById('quantity').value;
const date = document.getElementById('date').value;
const status = document.getElementById('status').value;
const record = { productName, quantity, date, status };
records.push(record);
localStorage.setItem('manufacturingRecords', JSON.stringify(records));
renderTable();
form.reset();
showNotification('Record added successfully', true);
}
// Function to handle table actions (edit, delete)
tableBody.addEventListener('click', function (e) {
if (e.target.classList.contains('delete-btn')) {
const index = e.target.getAttribute('data-index');
records.splice(index, 1);
localStorage.setItem('manufacturingRecords', JSON.stringify(records));
renderTable();
}
if (e.target.classList.contains('edit-btn')) {
const index = e.target.getAttribute('data-index');
const record = records[index];
document.getElementById('product-name').value = record.productName;
document.getElementById('quantity').value = record.quantity;
document.getElementById('date').value = record.date;
document.getElementById('status').value = record.status;
records.splice(index, 1);
renderTable();
}
});
// Add form submission handler
form.addEventListener('submit', addRecord);
// Initial render
renderTable();
});
function showNotification(message, isSuccess) {
const notification = document.getElementById("notification");
notification.className = isSuccess ? 'notification-success' : 'notification-error';
notification.innerText = message;
notification.style.display = "block";
setTimeout(() => { notification.style.display = "none"; }, 3000);
}