Skip to content

Commit 59acc5e

Browse files
author
Chung Nguyen
committed
update handle search feature
1 parent 20ce02b commit 59acc5e

File tree

3 files changed

+119
-15
lines changed

3 files changed

+119
-15
lines changed

assets/css/style.css

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ body {
2020
color: #333;
2121
}
2222

23+
main {
24+
max-width: 1200px;
25+
min-height: 680px;
26+
margin: 15px auto;
27+
padding: 20px;
28+
}
29+
2330
header {
2431
background-color: #fff;
2532
box-shadow: 0 5px 5px rgba(0, 0, 0, 0.1);
@@ -92,7 +99,7 @@ nav a {
9299
}
93100

94101
.search-container {
95-
max-width: 350px;
102+
width: 350px;
96103
z-index: 1;
97104
}
98105

@@ -102,11 +109,12 @@ nav a {
102109
}
103110

104111
.search-container input[type="text"] {
105-
flex: 1;
106112
padding: 10px;
113+
margin-right: -5px;
107114
outline: none;
108-
font-size: 16px;
115+
font-size: 14px;
109116
border: 1px solid #ccc;
117+
width: 250px;
110118
}
111119

112120
.search-container button {
@@ -129,11 +137,21 @@ nav a {
129137
margin-right: 10px;
130138
}
131139

132-
main {
133-
max-width: 1200px;
134-
min-height: 600px;
135-
margin: 15px auto;
136-
padding: 20px;
140+
.select-dropdown {
141+
position: absolute;
142+
background-color: #eee;
143+
border-radius: 4px;
144+
}
145+
146+
.select-dropdown option {
147+
font-size: 12px;
148+
max-width: 250px;
149+
padding: 8px 24px 8px 10px;
150+
border: none;
151+
background-color: transparent;
152+
-webkit-appearance: none;
153+
-moz-appearance: none;
154+
appearance: none;
137155
}
138156

139157
.certificate {
@@ -146,7 +164,8 @@ main {
146164
}
147165

148166
#pdfViewer {
149-
width: 100%; /* Đảm bảo canvas co giãn theo section */
167+
width: 100%;
168+
/* Đảm bảo canvas co giãn theo section */
150169
height: auto;
151170
display: block;
152171
margin: auto;
@@ -307,6 +326,7 @@ footer {
307326
from {
308327
opacity: 0;
309328
}
329+
310330
to {
311331
opacity: 1;
312332
}
@@ -321,19 +341,23 @@ footer {
321341
margin-right: 0;
322342
margin-bottom: 20px;
323343
}
344+
324345
#error-message {
325346
font-size: 16px;
326347
}
348+
327349
.footer {
328350
flex-direction: column;
329351
margin: 0 10px;
330352
}
353+
331354
.mobile-menu {
332355
display: block;
333356
position: absolute;
334357
top: 0;
335358
right: 0;
336359
}
360+
337361
nav ul {
338362
position: absolute;
339363
top: 50px;
@@ -342,16 +366,18 @@ footer {
342366
margin: 5px;
343367
display: none;
344368
}
369+
345370
nav ul li {
346371
background-color: #f2f2f2;
347372
color: #000;
348373
box-shadow: 0 5px 5px rgba(0, 0, 0, 0.2);
349374
}
375+
350376
nav .search-container {
351377
position: absolute;
352378
top: 50px;
353379
right: 0;
354380
animation: fadeIn 0.5s ease;
355381
display: none;
356382
}
357-
}
383+
}

assets/js/search-bar.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
document.addEventListener("DOMContentLoaded", async function () {
2+
const searchInput = document.getElementById("searchInput");
3+
const searchButton = document.getElementById("searchButton");
4+
const searchResults = document.getElementById("searchResults");
5+
6+
let data = {};
7+
8+
// Tải dữ liệu từ datasheet.json
9+
async function loadData() {
10+
try {
11+
const response = await fetch("./datasheet.json");
12+
data = await response.json();
13+
} catch (error) {
14+
console.error("Lỗi tải dữ liệu:", error);
15+
}
16+
}
17+
await loadData();
18+
19+
// Tìm kiếm theo ID, khóa học hoặc tên người
20+
function search(query) {
21+
query = query.toLowerCase();
22+
let results = [];
23+
24+
for (let id in data) {
25+
let courseTitle = data[id].courseTitle;
26+
let certifiedName = data[id].certifiedName;
27+
28+
if (id.toLowerCase().includes(query) || courseTitle.toLowerCase().includes(query) || certifiedName.toLowerCase().includes(query)) {
29+
results.push({ id, courseTitle, certifiedName });
30+
}
31+
}
32+
return results;
33+
}
34+
35+
// Cập nhật danh sách kết quả
36+
function updateDropdown(results) {
37+
searchResults.innerHTML = "";
38+
if (results.length === 0) {
39+
searchResults.style.display = "none";
40+
return;
41+
}
42+
43+
results.forEach(result => {
44+
let option = document.createElement("option");
45+
option.value = result.id;
46+
option.textContent = `${result.certifiedName} - ${result.courseTitle} (${result.id})`;
47+
searchResults.appendChild(option);
48+
});
49+
searchResults.style.display = "block";
50+
}
51+
52+
// Xử lý khi nhập vào ô tìm kiếm
53+
searchInput.addEventListener("input", function () {
54+
let query = searchInput.value.trim();
55+
if (query.length > 0) {
56+
let results = search(query);
57+
updateDropdown(results);
58+
} else {
59+
searchResults.style.display = "none";
60+
}
61+
});
62+
63+
// Chọn kết quả từ dropdown
64+
searchResults.addEventListener("change", function () {
65+
searchInput.value = searchResults.value; // Cập nhật ô tìm kiếm với ID đã chọn
66+
searchResults.style.display = "none";
67+
});
68+
69+
// Khi nhấn nút tìm kiếm
70+
searchButton.addEventListener("click", function () {
71+
let selectedId = searchInput.value.trim();
72+
if (data[selectedId]) {
73+
window.location.href = `?id=${selectedId}`;
74+
} else {
75+
alert("Không tìm thấy chứng chỉ!");
76+
}
77+
});
78+
});

index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@
3131
<li><a href="https://www.facebook.com/codingreshapefuture/">Liên hệ</a></li>
3232
</ul>
3333
<div class="search-container">
34-
<form action="#">
35-
<input type="text" placeholder="Tìm kiếm ...">
36-
<button type="submit"><i class="fa fa-search"></i></button>
37-
</form>
34+
<input type="text" id="searchInput" placeholder="Tìm kiếm Code ID, Course, Name,...">
35+
<button type="button" id="searchButton"><i class="fa fa-search"></i></button>
36+
<select id="searchResults" class="select-dropdown" size="5" style="display: none;"></select>
3837
</div>
3938
</nav>
4039
<div class="mobile-menu">
@@ -102,7 +101,8 @@ <h3>Title</h3>
102101
</footer>
103102
<script src="./assets/js/cert-render.js"></script>
104103
<script src="./assets/js/handle-button.js"></script>
104+
<script src="./assets/js/search-bar.js"></script>
105105
</body>
106106
<!-- Copyright by LTP 10/04/2023. All data is reversed -->
107107

108-
</html>
108+
</html>

0 commit comments

Comments
 (0)