mercredi 23 septembre 2020

Problem with checkbox function event ionic

I have a problem with my event on ion-checkbox.

I have a grocery list that I take from database and when I check a recipe I display the ingredients for the recipe. I have 2 recipe in the database and every recipe has 4 ingredients.

What I do right now , I display all the ingredients if I check the recipes and if an ingredient is present in both recipes i just add up the quantities.

I am stuck where I uncheck the ion-checkbox , because I get under the common ingredients that are in both recipes I get NaN when I tried to decrease the quantities so I can get the initial recipe ingredients.What it looks like in UI

My function code :

onCheckboxF(ev) {
    if (ev.detail.checked === true) {
for (let recipe of this.recipes) {
        if (recipe.name === ev.detail.value) {
          recipe.ingredients.forEach((singleIng) => {
            let matchedIng = this.groceryList.find(function (foundIng) {
              return foundIng.name === singleIng.name;
            });

            // console.log(this.groceryList);

            if (matchedIng) {
              this.untickedGroceryList.push(matchedIng);
              // console.log(this.untickedGroceryList);

              matchedIng.quantity = matchedIng.quantity + singleIng.quantity;
            } else {
              this.groceryList.push(singleIng);
            }
          });
        }
      }
    } else {
      let tickedRecipe = ev.detail.value;
      for (let recipe of this.recipes) {
        if (recipe.name === tickedRecipe) {
          recipe.ingredients.forEach((el) => {
            let matchedIng = this.groceryList.find(function (foundIng) {
              return foundIng.name === el.name;
            });
            if (matchedIng) {
              let index = this.groceryList.findIndex(
                (ingF) => ingF.name === matchedIng.name
              );
              this.groceryList.splice(index, 1);
            }
            let matchedSub = this.untickedGroceryList.find(function (
              foundIngred
            ) {
              return foundIngred.name === el.name;
            });

            if (matchedSub) {
              matchedSub.quantity = matchedSub - el.quantity;
              console.log(matchedSub);
            }
          });
        }
      }
    }
  }


As you can see I get NaN grams , the adding its working but to - the quantities its not working. What I am doing wrong , please help . Thank you for your time.




Aucun commentaire:

Enregistrer un commentaire