Skip to content

Commit be87aae

Browse files
authored
Merge pull request #20 from RichardHanitio/main
Added filter feature
2 parents 1b7b66e + b31ed5b commit be87aae

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@
5454
color: red;
5555
font-size: 18px;
5656
}
57+
58+
#filter-books {
59+
border-radius : 45px;
60+
border : none;
61+
outline : none;
62+
height : 38px;
63+
padding : 0 5px;
64+
}
65+
66+
#filter-books:focus{
67+
outline : 3px solid rgb(46, 123, 255);
68+
}
69+
5770
</style>
5871

5972
</head>
@@ -74,6 +87,12 @@
7487

7588
</ul>
7689
<form class="form-inline my-2 my-lg-0">
90+
<select name="filter" id="filter-books" class="form-select mr-sm-2">
91+
<option value="all" selected >All</option>
92+
<option value="book">Name</option>
93+
<option value="bookauthor">Author</option>
94+
<option value="bookType">Type</option>
95+
</select>
7796
<input class="form-control mr-sm-2" id="searchText" type="search" style="border-radius: 45px;"
7897
placeholder="Search">
7998
<button class="btn btn-dark my-2 my-sm-0 px-3" type="submit"

script.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ function displayBooks() {
106106
let index = 0;
107107

108108
objOfBook.forEach((books) => { //index is the length of the array
109-
110109
if (index == 0) {
111110
html += `
112111
<tr class="rows">
@@ -345,4 +344,82 @@ function showNumberOfBooks() {
345344
}
346345
}
347346

347+
// Filter books based on selected attributes from dropdown
348+
let filterDropdown = document.getElementById("filter-books");
349+
function filterBooks() {
350+
let books = JSON.parse(localStorage.getItem("shelfOfBooks"));
351+
// let numOfBooks = document.getElementById("books");
352+
let emptyMsg = document.getElementById("emptyMsg");
353+
let filterBy = filterDropdown.value;
354+
let html = "";
355+
let index = 0;
356+
let filteredBooks;
357+
if (filterBy === "all") {
358+
filteredBooks = books.filter((book) => {
359+
return (
360+
book.book
361+
.toLowerCase()
362+
.includes(searchNote.value.toLowerCase()) ||
363+
book.bookauthor
364+
.toLowerCase()
365+
.includes(searchNote.value.toLowerCase()) ||
366+
book.bookType
367+
.toLowerCase()
368+
.includes(searchNote.value.toLowerCase())
369+
);
370+
});
371+
} else {
372+
filteredBooks = books.filter((book) => {
373+
return book[filterBy]
374+
.toLowerCase()
375+
.includes(searchNote.value.toLowerCase());
376+
});
377+
}
378+
379+
if (filteredBooks.length > 0) {
380+
filteredBooks.forEach((filteredBook) => {
381+
html += `
382+
<tr class="rows">
383+
<th scope="row">${index + 1}</th>
384+
<td class="name">${filteredBook.book}</td>
385+
<td class="author">${filteredBook.bookauthor}</td>
386+
<td class="type">${filteredBook.bookType}</td>
387+
<td class="icon"><i class="fa fa-times" aria-hidden="true" onclick="removeBook(${index})"></i></td>
388+
</tr>
389+
`;
390+
index++;
391+
});
392+
emptyMsg.innerHTML = "";
393+
} else {
394+
let bookAttr;
395+
switch (filterBy) {
396+
case "all":
397+
bookAttr = "";
398+
break;
399+
case "book":
400+
bookAttr = "name";
401+
break;
402+
case "bookauthor":
403+
bookAttr = "author";
404+
break;
405+
case "bookType":
406+
bookAttr = "type";
407+
break;
408+
}
409+
410+
emptyMsg.innerHTML = `No book ${
411+
bookAttr !== "" ? "with" : ""
412+
} ${bookAttr} "${searchNote.value}" found`;
413+
}
414+
415+
// ? Does the number of books depends on the search results?
416+
// numOfBooks.innerHTML = "No. of Books: " + filteredBooks.length;
417+
418+
let table = document.getElementById("tableBody");
419+
table.innerHTML = html;
420+
}
421+
filterDropdown.addEventListener( "change", filterBooks);
422+
searchNote.addEventListener("input", filterBooks);
423+
424+
348425
showNumberOfBooks();

0 commit comments

Comments
 (0)