Skip to content

Commit d6eb3fe

Browse files
Merge pull request #1927 from isid555/search_2
Implemented Search Bar Functionality
2 parents e62ea0e + c95d914 commit d6eb3fe

File tree

3 files changed

+64
-8
lines changed

3 files changed

+64
-8
lines changed

index.html

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ <h2>30 Days of JavaScript</h2>
9292
<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">
9393
<button class="btn active">Learn JS</button>
9494
</a>
95+
<div>
96+
<input type="text" id="search-bar" placeholder="Search" />
97+
</div>
9598
</div>
9699
</div>
97100
</section>

script.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const projectsArea = document.querySelector(".project-list-area");
2-
2+
const searchBar = document.getElementById("search-bar");
3+
let allProjects = []
34
function getImagePath(id) {
45
return `public/assets/${id}.png`;
56
}
@@ -65,6 +66,16 @@ function getProjectContent(project) {
6566
}
6667

6768
function renderProjectList(projects) {
69+
projectsArea.innerHTML = ""
70+
71+
if (projects.length === 0) {
72+
const noResultsMessage = document.createElement("p");
73+
noResultsMessage.className = "no-results";
74+
noResultsMessage.innerText = "No results found";
75+
projectsArea.appendChild(noResultsMessage);
76+
return;
77+
}
78+
6879
projects.forEach((project) => {
6980
const projectCard = document.createElement("div");
7081
projectCard.className = "project-card";
@@ -86,15 +97,35 @@ function renderProjectList(projects) {
8697
});
8798
}
8899

100+
function filterProjects(query) {
101+
const filteredProjects = allProjects.filter(project =>
102+
project.name.toLowerCase().includes(query.toLowerCase()) ||
103+
project.description.toLowerCase().includes(query.toLowerCase())
104+
);
105+
106+
renderProjectList(filteredProjects);
107+
}
108+
89109
function fetchProjects() {
110+
90111
fetch("./data.json")
91-
.then((res) => res.json())
92-
.then((projects) => {
93-
renderProjectList(projects);
94-
})
95-
.catch((err) => {
96-
console.log("Error fetching project data:", err);
97-
});
112+
.then((res) => res.json())
113+
.then((projects) => {
114+
allProjects = projects;
115+
renderProjectList(projects);
116+
searchBar.addEventListener( "input", () => {
117+
const query = searchBar.value;
118+
if (query) {
119+
filterProjects(query)
120+
}
121+
else {
122+
renderProjectList(projects);
123+
}
124+
});
125+
})
126+
.catch((err) => {
127+
console.log("Error fetching project data:", err);
128+
});
98129
}
99130

100131
fetchProjects();

style.css

+22
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ section.hero {
167167
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
168168
gap: 25px;
169169
}
170+
#search-bar{
171+
height: 35px;
172+
width: 100%;
173+
max-width: 410px;
174+
border-radius: 8px;
175+
margin: 18px;
176+
padding: 8px;
177+
color: black;
178+
font-weight: bold;
179+
}
180+
#search-bar::placeholder{
181+
font-weight: bold;
182+
color: black;
183+
font-size: 13px;
184+
}
185+
.no-results {
186+
font-size: 1.5rem;
187+
color: black;
188+
text-align: center;
189+
padding: 20px;
190+
font-weight: bold;
191+
}
170192

171193
.project-card {
172194
border: 1px solid black;

0 commit comments

Comments
 (0)