Conditional Filtering by Other Dataset

Question:
Do I need to use code for this, or am I overlooking a simple solution? If code is needed, what functions should I look into as a newcomer to Wix and what’s the correct approach? I’m hoping to take two collections with some shared info and apply basic logic about that shared info to filter what entries are displayed on a dynamic list page.

Product:
Wix Editor

What are you trying to achieve:
Specifically, I have two collections: one is a collection of recipes that each have multiple ingredients, and the second is a collection of individual ingredients that each have a boolean state (checkbox) indicating whether it is in stock. I would like a dynamic list page to display only the recipes for which EVERY required ingredient is in stock, so that as I update the collection for ingredients the list page will only show recipes that are possible with ingredients on hand. Right now I have two collections with multi-reference fields, so that every recipe references ingredients needed, and every ingredient references all the recipes it’s used in.

What have you already tried:
I’ve reviewed all the guide I could find on dynamic pages, filtering datasets, guides on using reference fields and multi-reference fields. I looked at Velo guides for anything that clearly applies and I’m not sure where to start. I tried creating an “In Stock Ingredients” dataset from the ingredients collection that filters based on “in stock” status True, creating a “Possible Recipes” dataset from the recipes collection that filters based on including the referenced item in the “In Stock Ingredients” ingredients dataset, and then connecting the repeater on the dynamic list page to the “Possible Recipes” dataset. However, something isn’t working here because the repeater shows no valid entries. I figured it was worth a try, but there’s no reason to assume it would even behave the way I’m hoping.

Additional information:
I assume this is a pretty straightforward request and I’m surprised code might be required, so maybe I’m overlooking something in the basic functionality of dynamic pages. I bet there are examples out there doing exactly this in code, but I don’t know the right search terms and I’ve run out of leads searching tutorials.

I don’t have a tutorial to share but this would be possible with code. Essentially you’d do something like:

  1. Query to the ingredients collection for what is in stock. Something like await wixData.query("Ingredients").eq("inStock", true).find()
  2. Query the recipes to find items that at least have some of the ingredients you want.
  3. Further filter the recipe array with something like
let results = recipes.forEach((r) => {
  if(r.ingredients.every((i) => {ingredients.includes(i)})) {
    return r;
  }
})
  1. Display those results in a repeater myRepeater.data = results

This would be a bit involved though and definitely more coding than the general steps I mentioned above but hopefully this gets you started in the right direction.

Hi Anthony, thanks very much for the reply! It’s good to know I should focus on coded solutions, and this gives me a structure to work with and some API references to follow up on. I’ll try to fill in the gaps to get it working and report back.

1 Like