Display value from collection based on 2 criteria

Hello All,

I’m not very experienced writing code and was wondering if someone could point me in the right direction for the following:

My page has two dropdown fields, #dropdown1 for “country” and #dropdown2 for “name”.
#MyDataset has the fields “country”, “name”, and “food”.
I would like an on_click event in #textbox1 to display the value of the food field based on country and name selected.

Does anyone have any sample code that finds the “food” entry based on #dropdown1 and #dropdown2 values selected?

Help is much appreciated!
Greg

Hello Greg

here’s the steps of achieving that:

  • get the selected value from #dropdown1 → country
  • get the selected value from #dropdown2 → city
  • query you collection to find out the result that the country and city fields of it match the ones selected
  • get the food field value of the result
  • set the #textbox1 text to it

To know how to do those steps read about:

here’s a snippet that will help understanding the steps:

export async function dropdown2_change(event) {
    let country = $w('#dropdown1').value
    let city = $w('#dropdown2').value 

    let result = await wixData.query("collection").eq("country", country).eq('city', city).find()

    let food = result.items[0].neeee
    $w('#text1').text= food;
}

However, here i used dropdown2_change, this depends on when and how you want this to happen.

Best
Massa

Hi Massa,
Thank you so much! This already brought me quite a step further and I get the logic of what to do. However, still struggling to get it to work. The result is not displayed in the field. (#text1i is an input field). Here is my code - do you see anything off?

I think the issue may be related to how the value of the MyFoodField in the collection is read.

//what does this really return?
let food = result.items[0].neeee

It should return the value of the MyFoodField.

Update: I got it to work now. The following change did the trick:

let food = result.items[0].MyFoodField

Thanks a lot for pointing me in the right direction.