Learn how to effectively showcase your product range
// The following array consists of objects that represent various menu items
let products = [
{id:0, name:"Burger", price:150, img:"assets/images/burger.jpg", type:"foods"},
{id:1, name:"pizza", price:150, img:"assets/images/pizza.jpg", type:"foods"},
{id:2, name:"fries", price:150, img:"assets/images/fries.jpg", type:"foods"},
{id:3, name:"strawberry", price:150, img:"assets/images/strawberry.jpg", type:"juices"},
{id:4, name:"apple", price:150, img:"assets/images/apple.jpg",type:"juices"},
{id:5, name:"orange", price:150, img:"assets/images/orange.jpg", type:"juices"}
]
// Initialize an empty array to store filtered items based on user choice (FOODS or JUICES)
let filtered = []
// Identify the input where the user selects FOODS or JUICES
let input = document.querySelectorAll(".ma__filter__item")
// Perform filtering of products based on user selection
if(input.value == "foods"){
var results = products.filter(prod => (prod.type ==="foods"))
filtered = [...results]
}
else if(input.value == "juices"){
var results = products.filter(prod => (prod.type === "juices"))
filtered = [...results]
}
for(let i=0 ; i < filtered.length ; ++i){
console.log(filtered[i])
newproduct(filtered[i])
}
// Locate the div for displaying the items
let grid = document.querySelector("#ma__grid");
grid.innerHTML = "" ;
// Function to create a new div, populate it with product data and append it to the display grid
function newproduct(prod){
let newProd = document.createElement("div");
newProd.className = "ma__grid__item";
newProd.innerHTML = `<div class="card" style="width: 18rem;">
<img class="card-img-top crd_img" src=${prod.img} alt="Card image cap">
<div class="card-body container">
<div class="row ma__prod__name">
<h2>${prod.name}<h2>
</div>
<div class="row ma__prod__details">
<p>Lorem ipsum dolor, sit amet consectetur llitia. Quo</p>
</div>
<div class="row ma__prod__name text-center">
<p class="col-lg-12">price : ${prod.price}$</p>
</div>
<div class="row ma__prod__name d-flex justify-content-center">
<button name ="${prod.id}" type="button" class="btn btn-primary ma__prod__btn ma__prod__btn1">Add To Cart</button>
<button type="button" class="btn btn-primary ma__prod__btn ma__prod__btn2">More Details</button>
</div>
</div>
</div> `;
grid.appendChild(newProd);
}