Skip to content

Commit aad6b9d

Browse files
committed
ajout de filtre pendant la recherche
1 parent 6f1c05a commit aad6b9d

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

data/courses.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const COURSES = [
2-
{id: 1, img: 'ux_ui.jpg', title: 'UX/UI', slug: 'ux_ui', initial_price: 200, price: 9.99, mark: 4, stock: 10},
3-
{id: 2, img: 'php_8.png', title: 'PHP 8', slug: 'php_8',initial_price: 200, price: 9.99, mark: 5, stock: 10},
4-
{id: 3, img: 'react_js.png', title: 'React JS', slug: 'react_js', initial_price: 200, price: 9.99, mark: 2, stock: 5},
5-
{id: 4, img: 'node_js.jpg', title: 'Node JS', slug: 'node_js', initial_price: 200, price: 9.99, mark: 5, stock: 3},
6-
{id: 5, img: 'my_sql.png', title: 'MySQL', slug: 'mysql', initial_price: 200, price: 9.99, mark: 4, stock: 2}
7-
]
2+
{ id: 1, img: 'ux_ui.jpg', title: 'UX/UI', slug: 'ux_ui', initial_price: 200, price: 17.99, mark: 4, stock: 10 },
3+
{ id: 2, img: 'php_8.png', title: 'PHP 8', slug: 'php_8', initial_price: 200, price: 20.99, mark: 5, stock: 10 },
4+
{ id: 3, img: 'react_js.png', title: 'React JS', slug: 'react_js', initial_price: 200, price: 12.99, mark: 2, stock: 5 },
5+
{ id: 4, img: 'node_js.jpg', title: 'Node JS', slug: 'node_js', initial_price: 200, price: 10.99, mark: 1, stock: 3 },
6+
{ id: 5, img: 'my_sql.png', title: 'MySQL', slug: 'mysql', initial_price: 200, price: 14.99, mark: 3, stock: 2 }
7+
]

js/search.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
// search bar
22

3-
document.querySelector('form.search-form input').addEventListener('input', function (e) { // If a change in the search bar is detected, run the function
4-
3+
document.querySelector('form.search-form input').addEventListener('input', function(e) { // If a change in the search bar is detected, run the function
4+
search(e.target.value);
5+
});
6+
7+
function search(e) {
58
var input, filter; // Inizialize variables
6-
input = e.target.value; // Get the value of the search bar
7-
filter = input.toUpperCase(); // Convert the value to uppercase
9+
filter = e.toUpperCase(); // Convert the value to uppercase
810

911
document.querySelector("#courses-list .courses__container").innerHTML = ""; // Clear results
1012

11-
if(input){ // If the input is not empty
12-
for(let i = 0; i < COURSES.length; i++){ // Loop through all courses
13-
if(COURSES[i].title.toUpperCase().includes(filter)){ // If the course title includes the value of the search bar
13+
if (e) { // If the input is not empty
14+
for (let i = 0; i < COURSES.length; i++) { // Loop through all courses
15+
if (COURSES[i].title.toUpperCase().includes(filter)) { // If the course title includes the value of the search bar
1416
productItems('#courses-list .courses__container', COURSES[i]); // Show the product
1517

1618
document.querySelector('#no_course').classList.add('hidden'); // Hide the no results message
17-
} else if(!document.querySelector("#courses-list .courses__container").innerHTML){ // Otherwise, if there are no results
19+
} else if (!document.querySelector("#courses-list .courses__container").innerHTML) { // Otherwise, if there are no results
1820
document.querySelector('#no_course').classList.remove('hidden'); // Show the no results message
1921
}
20-
}
22+
}
2123
} else { // Otherwise, if the input is empty
2224
showAllProducts('.courses__container', COURSES); // Show all products
2325
}
24-
});
26+
}
2527

2628
function filtre(type) { // Search filter function
27-
if(type === "noteCroissant"){ // If the filter is note croissant
29+
if (type === "noteCroissant") { // If the filter is note croissant
2830
COURSES.sort((a, b) => a.mark - b.mark) // Sort the courses by note
29-
} else if(type === "noteDecroissant"){ // If the filter is note decroissant
31+
} else if (type === "noteDecroissant") { // If the filter is note decroissant
3032
COURSES.sort((a, b) => b.mark - a.mark) // Sort the courses by note
31-
} else if(type === "prixCroissant"){ // If the filter is prix croissant
33+
} else if (type === "prixCroissant") { // If the filter is prix croissant
3234
COURSES.sort((a, b) => a.price - b.price) // Sort the courses by price
33-
} else if(type === "prixDecroissant"){ // If the filter is prix decroissant
35+
} else if (type === "prixDecroissant") { // If the filter is prix decroissant
3436
COURSES.sort((a, b) => b.price - a.price) // Sort the courses by price
3537
}
3638

37-
document.querySelector('.courses__container').innerHTML = ""; // Clear products
38-
showAllProducts('.courses__container', COURSES); // Show all products
39-
}
39+
if (document.querySelector('form.search-form input').value) {
40+
search(document.querySelector('form.search-form input').value);
41+
} else {
42+
document.querySelector('.courses__container').innerHTML = ""; // Clear products
43+
showAllProducts('.courses__container', COURSES); // Show all products
44+
}
45+
}

0 commit comments

Comments
 (0)