Dropdown setting options for another dropdown

one dropdown sets the machine

one dropdown sets the products

I don’t want to have a possible product on a machine it can’t run

So I am already thinking binding the property to get a look at which machine is selected

I then don’t know how to return the option set though

I am reading

I think it covers it, not sure yet.

that seems like it was maybe much older

I think I can go to value, bind that to the property

add a transform script with a list for each option, plug in the value, spit-out the value I want?

It is alive

maybe the database way is better in some ways?

I have used the database approach. For my purposes its Buildings/Tenants vs Machine/Parts, but the method is the same. The first dropdown has a list of locations, the second one has a query binding that retrieves the list of tenants for the selected building using a parameter containing the selected building.

The database approach is going to be far more resilient in the long run. Supposing you have a DB where you have three tables MACHINES, PRODUCTS, and MP_CORR, then I recommend something like this:

Bind the options for Dropdown_0 to a Named Query, which has something like this (I also recommend using the JSON option instead of “auto”):

select id, name from MACHINES

and use a transform:

return [{"label": machine.name, "value": machine.id} for machine in value]

Then bind the options of Dropdown_1 with the same general approach.
Named Query (parameterized):

select id, name from product where id in (select product_id from MP_CORR where machine_id=:machineId)

This might seem like a lot of overhead, but imagine trying to lock down all of the places in your project which might need to be updated as you add/remove machines/products/locations. You should never hard-code that sort of product information into a project.

2 Likes

I will attempt this tomorrow.

What is MP_Corr?
How does the for machine in value part work in the return?

I will make one relatively static table for this instead of the lists then.
Thanks for the help

MP_CORR is an assumed table which correlates machine/product relationships

MACHINES
id | name
0  | Machine0
1  | Machine1

PRODUCTS
id | name
0  | Product0
1  | Product1

MP_CORR
machine_id | product_id
0          | 0
0          | 1
1          | 1

Would then represent a system where there exists two machines and two products, and Machine0 uses both Product0 and Product1, whereas Machine1 only uses Product1. Without some sort of table which stores the relationship between machines and products you won’t be able to set this up.

That’s a bit of list comprehension from python. Essentially the transform will iterate over every item in the query (which has been passed into the transform as value), and alias the “item” as “machine”, before converting the “machine” item into a dictionary which has two keys (“label” and “value”) and assigning machine.name and machine.id as the values of those keys.

1 Like

It looks like next week I will get to it
Thanks for explaining