-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-orders.html
More file actions
174 lines (150 loc) · 5.31 KB
/
Copy pathadmin-orders.html
File metadata and controls
174 lines (150 loc) · 5.31 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Manage Orders</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container my-5">
<h2 class="text-center mb-4">Manage All Orders</h2>
<div class="mb-3 row">
<div class="col-md-4">
<input type="text" id="search-user" class="form-control" placeholder="Search by user name" />
</div>
<div class="col-md-4">
<select id="filter-status" class="form-select">
<option value="">All Statuses</option>
<option value="Pending">Pending</option>
<option value="Shipped">Shipped</option>
<option value="Delivered">Delivered</option>
</select>
</div>
</div>
<div id="orders-container" class="table-responsive">
<table class="table table-bordered table-hover">
<thead class="table-light">
<tr>
<th>Order ID</th>
<th>User</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="orders-table-body">
<!-- Orders will be populated here -->
</tbody>
</table>
</div>
<nav>
<ul class="pagination justify-content-center mt-4" id="pagination">
<!-- Pagination will be added here -->
</ul>
</nav>
<div id="status-message" class="mt-4 text-center"></div>
</div>
<script>
const ORDERS_PER_PAGE = 5;
let orders = [];
let currentPage = 1;
function generateFakeOrders(users) {
return users.map((user, index) => ({
id: index + 1,
userName: user.name,
status: ["Pending", "Shipped", "Delivered"][Math.floor(Math.random() * 3)]
}));
}
async function fetchOrders() {
const res = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await res.json();
orders = generateFakeOrders(users);
renderTable();
renderPagination();
}
function renderTable() {
const tbody = document.getElementById('orders-table-body');
tbody.innerHTML = "";
const filtered = applyFilters(orders);
const start = (currentPage - 1) * ORDERS_PER_PAGE;
const paginatedOrders = filtered.slice(start, start + ORDERS_PER_PAGE);
if (paginatedOrders.length === 0) {
tbody.innerHTML = '<tr><td colspan="4" class="text-center">No orders found</td></tr>';
return;
}
for (const order of paginatedOrders) {
const row = document.createElement('tr');
row.innerHTML = `
<td>${order.id}</td>
<td>${order.userName}</td>
<td>${order.status}</td>
<td>
<button class="btn btn-sm btn-info" onclick="viewOrder(${order.id})">View</button>
<button class="btn btn-sm btn-warning" onclick="updateOrder(${order.id})">Update</button>
<button class="btn btn-sm btn-danger" onclick="deleteOrder(${order.id})">Delete</button>
</td>
`;
tbody.appendChild(row);
}
}
function renderPagination() {
const pagination = document.getElementById('pagination');
pagination.innerHTML = "";
const filtered = applyFilters(orders);
const totalPages = Math.ceil(filtered.length / ORDERS_PER_PAGE);
for (let i = 1; i <= totalPages; i++) {
const li = document.createElement('li');
li.className = `page-item ${i === currentPage ? 'active' : ''}`;
li.innerHTML = `<button class="page-link" onclick="changePage(${i})">${i}</button>`;
pagination.appendChild(li);
}
}
function changePage(page) {
currentPage = page;
renderTable();
renderPagination();
}
function viewOrder(id) {
alert(`View details for order ID: ${id}`);
}
function updateOrder(id) {
alert(`Update order ID: ${id}`);
}
function deleteOrder(id) {
if (confirm(`Are you sure you want to delete order ID: ${id}?`)) {
orders = orders.filter(order => order.id !== id);
currentPage = 1;
renderTable();
renderPagination();
showMessage(`Order ID ${id} deleted successfully`, 'danger');
}
}
function showMessage(msg, type = 'info') {
const div = document.getElementById('status-message');
div.innerHTML = `<div class="alert alert-${type}">${msg}</div>`;
setTimeout(() => (div.innerHTML = ""), 3000);
}
function applyFilters(orderList) {
const searchValue = document.getElementById('search-user').value.toLowerCase();
const statusValue = document.getElementById('filter-status').value;
return orderList.filter(order => {
const matchesName = order.userName.toLowerCase().includes(searchValue);
const matchesStatus = statusValue === "" || order.status === statusValue;
return matchesName && matchesStatus;
});
}
document.getElementById('search-user').addEventListener('input', () => {
currentPage = 1;
renderTable();
renderPagination();
});
document.getElementById('filter-status').addEventListener('change', () => {
currentPage = 1;
renderTable();
renderPagination();
});
// Start
fetchOrders();
</script>
</body>
</html>