|
| 1 | +## Filters Project |
| 2 | + |
| 3 | +#### HTML Structure |
| 4 | + |
| 5 | +- section.products |
| 6 | + |
| 7 | + - div.filters-container |
| 8 | + - div.products-container |
| 9 | + |
| 10 | +- .filters-container |
| 11 | + |
| 12 | + - form.input-form |
| 13 | + - input.search-input |
| 14 | + - h5(company) |
| 15 | + - article.companies |
| 16 | + - button.company-btn(temp values) |
| 17 | + |
| 18 | +- .products-container |
| 19 | + - article.product |
| 20 | + - img.product-img.img (src from products.js) |
| 21 | + - footer |
| 22 | + - h5.product-name(name) |
| 23 | + - span.product-price(price) |
| 24 | + |
| 25 | +#### Display Products |
| 26 | + |
| 27 | +- import products |
| 28 | +- make a copy and assign to new variable (use let keyword) |
| 29 | +- select .products-container |
| 30 | +- setup a function displayProducts, iterate over products, display them |
| 31 | + |
| 32 | +#### Filter Based On Text |
| 33 | + |
| 34 | +- select form, input |
| 35 | +- listen for 'keyup' events on form |
| 36 | +- get input value |
| 37 | +- filter based on the input value |
| 38 | +- call displayProducts |
| 39 | + |
| 40 | +```js |
| 41 | +// Text Filter |
| 42 | + |
| 43 | +const form = document.querySelector('.input-form'); |
| 44 | +const searchInput = document.querySelector('.search-input'); |
| 45 | + |
| 46 | +form.addEventListener('keyup', () => { |
| 47 | + // keyup - fired when key released |
| 48 | + const inputValue = searchInput.value; |
| 49 | + filteredProducts = products.filter((product) => { |
| 50 | + return product.title.toLowerCase().includes(inputValue); |
| 51 | + }); |
| 52 | + displayProducts(); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +#### Empty Array |
| 57 | + |
| 58 | +- displayProducts() |
| 59 | +- check length of filteredProducts |
| 60 | +- if list.length < 1 |
| 61 | +- set productsContainer = some text |
| 62 | + |
| 63 | +#### Display Filter Buttons |
| 64 | + |
| 65 | +- select .companies |
| 66 | +- create function displayButtons |
| 67 | +- get only unique companies (set) |
| 68 | +- iterate over results |
| 69 | +- return button with data-id |
| 70 | +- set .companies innerHTML equal to result |
| 71 | + |
| 72 | +#### Filter Based on Company |
| 73 | + |
| 74 | +- add event listener on .companies |
| 75 | +- look for event.target |
| 76 | +- if contains .company-btn proceed |
| 77 | + - if 'all' return all products (copy) |
| 78 | + - else filter based on company value |
| 79 | +- set search value '' |
| 80 | +- call displayProducts |
0 commit comments