Dependent Dropdown fields – Best Solution?

I am using nested IF formulas but I suspect there may be a better way. When I drop these formulas in (after creating them in Excel), it takes a few seconds for Composer to digest them, but more importantly, since I have been using them, when testing in Runtime 2.5.12, the app is taking more time to load. So it makes me think my solution is cumbersome even though the dropdowns work perfectly fine in the app. Is there a more efficient way to do what I want?

It is a fitness app. So I have two dropdowns on the same page, lets say, one is for β€˜Level’ and one is for β€˜Stage’. Stage is dependent on Level. For example, Level 1 has 22 Stages, Level 2 has 30.

Note in these dropdowns, label and value are always the same. I am linking the output of Level to a page variable.

For β€˜Level’, I have 1 to 10, so I can just use the dropdown functionality and populate as below;

For β€˜Stage’, I am using nested IF formulas that look like this;

IF(NUMBER(pageVars.Level) == 1,[{label:β€œ1”, value:β€œ1”},{label:β€œ2”, value:β€œ2”}, {label:β€œ3”, value:β€œ3”}, {label:β€œ4”, value:β€œ4”}, {label:β€œ5”, value:β€œ5”}, …….,{label:β€œ21”, value:β€œ21”},{label:β€œ22”, value:β€œ22”}],
IF(NUMBER(pageVars.Level) == 2,[{label:β€œ3”, value:β€œ3”}, {label:β€œ4”, value:β€œ4”}, {label:β€œ5”, value:β€œ5”}, {label:β€œ6”, value:β€œ6”},{label:β€œ7”, value:β€œ7”},…….,{label:β€œ29”, value:β€œ29”}, {label:β€œ30”, value:β€œ30”},
IF(NUMBER(pageVars.Level) ==3 ……………

These formulas become quite long, but they work. Is there a more efficient way to do this, and is the size of these formulas making the app take longer to load?
I thought perhaps I could use a database to store the array, and reference that, but I don’t know where to start in terms of getting a database to store an array (I am using Backendless…)
Thanks
Paul

Hi Paul, you could store the number of stages in a page variable as well, and then create the list of stages dynamically:

Assuming pageVars.Stage = 3

First generate the range of numbers:
GENERATE_RANGE(1, pageVars.Stage) β†’ [1,2,3] (docs)

Then transform the numbers into objects:
MAP(GENERATE_RANGE(1, pageVars.Stage), {label: item, value: item}) β†’ [{label: 1, value: 1}, {label: 2, value: 2}, {label: 3, value: 3}] (docs)

1 Like

Thanks Mari

When I build formulas, I like to do it one step at a time and see the result…
When I put GENERATE_RANGE(1, 4) into a paragrpah component, it just returns a blank field. Is that right? I shouldn’t be able to preview the result?
When I put this into a dropdown label, MAP(GENERATE_RANGE(1,22), {label:item , value: item}) it crashes the app.

What am I missing here?

Hi Paul, the problem with binding lists or objects to paragraphs or other components that expect a string type is that even if objects and lists kind of look like texts in the formula editor, they are not and cannot be easily converted to a text.

What I usually do to preview my formula results is that I set a paragraph / other suitable component to repeat with the formula, and then bind the content to a text/number property in the repeat, in this case current.label or current.value.

I got this to work by binding the MAP formula to the β€œOption list” property of a dropdown:

OK, am I being dumb here? This is what I have, the formula is as follows;
MAP(GENERATE_RANGE(1,22), {label: item , value: item})

The app crashes…

It seems to be working with this;
MAP(GENERATE_RANGE(1,22), {label: STRING(item) , value: STRING(item)})

Is this what you did in your above example, add the STRING() ?

Thanks!

1 Like

Hi, for some reason it worked for me also without the STRING(). But glad that you were able to work it out!