• Resolved johncaspar

    (@johncaspar)


    Hi, I’m trying to build a food nutrition data calculator, which would show the calories, proteins, fats, and carbs for specific foods.

    And here is my question… Can I assign multiple values to a dropdown field element? For example, if I have: “MILK” in the dropdown list… So I could assign it multiple values for calories, proteins and etc…

    So the idea is to have a dropdown field with a list of foods and then a number field to insert the amount of food in grams… I would like to assign multiple values to the drop-down list (for calories, fats, proteins, carbs) and add values for 1g of food there… and then create a calculation field that would multiply the dropdown list element with the number (the grams) element…

    Hope you guys understand what I’m trying to do… Anyway is it possible this way? somehow?

    And if not could ya’ll give me a few pointers on how to achieve this 🙂

    Thanks!

Viewing 1 replies (of 1 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @johncaspar

    You have different alternatives, but the recommended one would be to generate a plain object with all the values and use the dropdown field as the attribute selector. I’ll try to describe the process with a hypothetical example:

    Assuming you have three foods: Food A, Food B, and Food C.

    1g of Food A contains:
    calories: 0.1,
    fats: 0.31,
    proteins: 0.2,
    carbs: 0.4

    1g of Food B contains:
    calories: 0.3,
    fats: 0.6,
    proteins: 0.12,
    carbs: 0.9

    1g of Food C contains:
    calories: 0.45,
    fats: 0.2,
    proteins: 0.8,
    carbs: 0.12

    Now, the text and values of the DropDown field (fieldname1) would be the products’ names (Field A, Field B, and Field C).

    Insert an “HTML Content” field in the form with a plain object as its content:

    
    <script>
    db = {
    'Food A': {calories: 0.1, fats: 0.31, proteins: 0.2, carbs: 0.4},
    'Food B': {calories: 0.3, fats: 0.6, proteins: 0.12, carbs: 0.9},
    'Food C': {calories: 0.45, fats: 0.2, proteins: 0.8, carbs: 0.12}
    };
    </script>
    

    If the number field for the weight is the fieldname2, the equation of the calculated field for calories would be:

    db[fieldname1]['calories']*fieldname2

    The equation of the calculated field for fats would be:

    db[fieldname1]['fats']*fieldname2

    The equation of the calculated field for proteins would be:

    db[fieldname1]['proteins']*fieldname2

    The equation of the calculated field for carbs would be:

    db[fieldname1]['carbs']*fieldname2

    Best regards.

Viewing 1 replies (of 1 total)

The topic ‘Assigning value to dropdown field.’ is closed to new replies.