filter products

This commit is contained in:
soyed 2021-02-17 15:28:52 -05:00
commit f992656570
3 changed files with 75 additions and 71 deletions

View file

@ -25,30 +25,28 @@
<!-- Tab content --> <!-- Tab content -->
<div id="Client" class="tabcontent"> <div id="Client" class="tabcontent">
<h3>Client Information</h3> <h3>Client Information</h3>
<p> <p>Hello, welcome to our grocery store. <br>
Hello, welcome to our grocery store. We are here to help you choose the perfect groceries.</p>
<br />
We are here to help you choose the perfect groceries for your store.
</p>
<i><strong>Please Choose a Category:</strong></i> <i><strong>Please Choose a Category:</strong></i>
<select
id="dietSelect" <br>
name="dietSelect" <label for="category-foods1"> Vegetarian</label>
class="sumo" <input type="checkbox" id ="Vegetarian" value="Vegetarian" onchange="populateListProductChoices(this.id, 'displayProduct')">
onchange="populateListProductChoices(this.id, 'displayProduct')" <br>
>
<option value=""></option>
<option value="Vegetarian">Vegetarian</option> <label for="category-foods1"> Gluten-Free</label>
<option value="GlutenFree">Gluten Free</option> <input type="checkbox" id="GlutenFree" value="GlutenFree" onchange="populateListProductChoices(this.id, 'displayProduct')">
<option value="VegetarianANDGluten-Free"> <br>
Vegetarian and Gluten-Free <label for="category-foods1"> Organic</label>
</option> <input type="checkbox" id="Organic" value="Organic" onchange="populateListProductChoices(this.id, 'displayProduct')">
<option value="Organic">Organic</option> <br>
<option value="Non-organic">Non-Organic</option> <label for="category-foods1"> Non-Organic</label>
<option value="None">None</option> <input type="checkbox" id="Non-Organic" value="Non-Organic" onchange="populateListProductChoices(this.id, 'displayProduct')">
</select> <br>
<br /> <label for="category-foods1"> None</label>
<input type="checkbox" id="None" value="None" onchange="populateListProductChoices(this.id, 'displayProduct')">
<br>
<i><strong>Please Choose a Characteristic:</strong></i> <i><strong>Please Choose a Characteristic:</strong></i>
<select class="sumo" name="characterSelect"> <select class="sumo" name="characterSelect">
<option value=""></option> <option value=""></option>
@ -107,7 +105,10 @@
<div id="Cart" class="tabcontent"> <div id="Cart" class="tabcontent">
<h3>Shopping Cart</h3> <h3>Shopping Cart</h3>
<p>The items you have added to your list will appear here.</p> <p>The items you have added to your list will appear here.</p>
<div id="displayCart"></div> <div id="displayCart">
</div>
<div class="footer"> <div class="footer">
<p class="copyright"> <p class="copyright">
Website created by Ruchira, Sam, Batuhan, and Kene Website created by Ruchira, Sam, Batuhan, and Kene

View file

@ -161,13 +161,13 @@ function getProductImg(productName) {
function restrictListProducts(prods, restriction) { function restrictListProducts(prods, restriction) {
let product_names = []; let product_names = [];
for (let i=0; i<prods.length; i+=1) { for (let i=0; i<prods.length; i+=1) {
if ((restriction == "Vegetarian") && (prods[i].vegetarian == true)){ if ((restriction === "Vegetarian") && (prods[i].vegetarian == true)){
product_names.push(prods[i].name); product_names.push(prods[i].name);
} }
else if ((restriction == "GlutenFree") && (prods[i].glutenFree == true)){ else if ((restriction == "GlutenFree") && (prods[i].glutenFree == true)){
product_names.push(prods[i].name); product_names.push(prods[i].name);
} }
else if ((restriction == "VegetarianANDGluten-Free") && (prods[i].glutenFree == true) && (prods[i].vegetarian == true)){ else if ((restriction == "Vegetarian" && restriction == "GlutenFree") && (prods[i].glutenFree == true) && (prods[i].vegetarian == true)){
product_names.push(prods[i].name); product_names.push(prods[i].name);
} }
else if ((restriction == "Organic") && (prods[i].organic == true)){ else if ((restriction == "Organic") && (prods[i].organic == true)){
@ -198,7 +198,7 @@ function filterProduct(prod, filter) {
else if ( filter === "Dairy" && prod[i].Dairy === true) { else if ( filter === "Dairy" && prod[i].Dairy === true) {
prod_filter.push(prod[i].name); prod_filter.push(prod[i].name);
} }
else if ( filter === "Meat&SeaFood" && prod[i].MeatandSeafood === true) { else if ( filter === "Meat&Seafood" && prod[i].MeatandSeafood === true) {
prod_filter.push(prod[i].name); prod_filter.push(prod[i].name);
} }
else if ( filter === "Bakery" && prod[i].Bakery === true) { else if ( filter === "Bakery" && prod[i].Bakery === true) {

View file

@ -23,57 +23,60 @@ function openInfo(evt, tabName) {
// it makes each product name as the label for the checkbos // it makes each product name as the label for the checkbos
function populateListProductChoices(slct1, slct2) { function populateListProductChoices(slct1, slct2) {
var s1 = document.getElementById(slct1) var s1 = document.getElementById(slct1);
var s2 = document.getElementById(slct2) var s2 = document.getElementById(slct2);
// s2 represents the <div> in the Products tab, which shows the product list, so we first set it empty
s2.innerHTML = "";
s2.appendChild(document.createElement("br"));
s2.appendChild(document.createElement("br"));
// obtain a reduced list of products based on restrictions
var optionArray = restrictListProducts(products, s1.value);
// s2 represents the <div> in the Products tab, which shows the product list, so we first set it empty // for each item in the array, create a checkbox element, each containing information such as:
s2.innerHTML = '' // <input type="checkbox" name="product" value="Bread">
s2.appendChild(document.createElement('br')) // <label for="Bread">Bread/label><br>
s2.appendChild(document.createElement('br'))
for (i = 0; i < optionArray.length; i++) {
var productName = optionArray[i];
// create the checkbox and add in HTML DOM
var checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = "product";
checkbox.value = productName;
s2.appendChild(checkbox);
// create a label for the checkbox, and also add in HTML DOM
var label = document.createElement('label')
label.htmlFor = productName;
var itemPrice = getItemPrice(productName);
label.appendChild(document.createTextNode(productName + " $" + itemPrice));
// obtain a reduced list of products based on restrictions // //get price of an item
var optionArray = restrictListProducts(products, s1.value) // var itemPrice = getItemPrice(productName);
// label.appendChild(document.createTextNode(" $" + itemPrice));
// for each item in the array, create a checkbox element, each containing information such as: s2.appendChild(label);
// <input type="checkbox" name="product" value="Bread">
// <label for="Bread">Bread/label><br> // create a breakline node and add in HTML DOM
s2.appendChild(document.createElement("br"));
//create <img src="">
var prodImg = document.createElement("img");
prodImg.src = getProductImg(productName);
// prodImg = document.getElementById("displayProduct");
// console.log("trial", prodImg.src);
s2.appendChild(prodImg);
for (i = 0; i < optionArray.length; i++) { //use this to space the images
var productName = optionArray[i] s2.appendChild(document.createElement("br"));
// create the checkbox and add in HTML DOM s2.appendChild(document.createElement("br"));
var checkbox = document.createElement('input')
checkbox.type = 'checkbox'
checkbox.name = 'product'
checkbox.value = productName
s2.appendChild(checkbox)
// create a label for the checkbox, and also add in HTML DOM }
var label = document.createElement('label')
label.htmlFor = productName
var itemPrice = getItemPrice(productName)
label.appendChild(document.createTextNode(productName + ' $' + itemPrice))
// //get price of an item
// var itemPrice = getItemPrice(productName);
// label.appendChild(document.createTextNode(" $" + itemPrice));
s2.appendChild(label)
// create a breakline node and add in HTML DOM
s2.appendChild(document.createElement('br'))
//create <img src="">
var prodImg = document.createElement('img')
prodImg.src = getProductImg(productName)
// prodImg = document.getElementById("displayProduct");
// console.log("trial", prodImg.src);
s2.appendChild(prodImg)
//use this to space the images
s2.appendChild(document.createElement('br'))
s2.appendChild(document.createElement('br'))
}
} }
// This function is called when the "Add selected items to cart" button in clicked // This function is called when the "Add selected items to cart" button in clicked
// The purpose is to build the HTML to be displayed (a Paragraph) // The purpose is to build the HTML to be displayed (a Paragraph)