Problem with conditional dynamic event

Wappler Version : 2.2.7
Operating System : Windows 10

Expected behavior

What do you think should happen?

Using conditional dynamic events should be possible whether or not there are multiple actions for each event.

Actual behavior

What actually happens?

If there is more than one action in the default event, nothing happens and a “Parse Error” appears in the developer console.

How to reproduce

If I click a button, I might want different things to happen depending on some condition. I can do something like this:
dmx-on:click="theCondition? defaultAction() : alternativeAction()"
and it works fine.

Frequently, there would be more than a single action. This works for the alternative action (as in this example), eg:
dmx-on:click="theCondition? defaultAction() : alternativeAction1() ; alternativeAction2() ; "

However if I use more than one action in the default event, an error occurs. Eg:
dmx-on:click="theCondition? defaultAction1() ; defaultAction2() : alternativeAction() "

It may be a syntax issue but I’ve tried putting brackets round various parts of the code but I can’t get it to work.

If i follow what you need, effectively if then else if then else you need to effectively stack the conditionals, cant test from where I am but syntax should be something like this
dmx-on:click=“theCondition? defaultAction1() : theotherCondition? defaultAction2() :defaultAction3()”

Careful with those colons, they need to be full, not semis

1 Like

Thanks - but it’s actually something simpler that I want to achieve, at least for this example. For example I might want to:
if a certain condition is true, set a variable and display a modal (one event/two actions)
if the condition is false, open another page

This doesn’t seem to be possible (I have checked the colons, semicolons very carefully). It works if I reverse things:
if the condition is true, open another page
if a certain condition is false, set a variable and display a modal (one event/two actions)

My example is actually much simpler than what I want to do. I would need to have multiple, nested, ternary operators. I was just testing the feasiblity of this and came across this problem.

Indeed, the syntax is wrong. You cannot use ; there, it must be as Brian explained :slight_smile:

I think you’re both misunderstanding me. In both cases, I’m using a colon to separate the ifTrue/ifFalse expression - not a semicolon. I’m using the semicolon to separate multiple actions - and this works, as expected, in the ifFalse part, but not in the ifTrue.

Perhaps split the double action into stages

if condition1?set variable: do nothing

the use another conditional: if condition1? action 1 : action2

Thanks again, I haven’t tried this but it could make things unnecessarily complicated. I would first like to confirm that it’s a bug.

To give an example of the sort of thing I mean. If the status has a certain value, I might want to display a modal, and set an ID to populate the modal (two actions). If the status has different value, I might want to display a different modal and again set an ID (also two actions). I think this should work:

dmx-on:click="theCondition? defaultAction1() ; defaultAction2() : alternativeAction1() ; alternativeAction2()"

It does, as long as there is only a single action in the first part (the default event).

I’m sorry Tom, but ternary operator syntax is not allowing you to use ; like that.
I really don’t seem to understand your idea, but ternary operator (which is not invented by Wappler) works with the following syntax:

condition ? actionTrue : actionFalse

The nested version would work like:

condition ? actionTrue : condition2 ? actionTrue : actionFalse

In Wappler ; separates different actions to be executed, not alternative ones.

It does work - but only in the ‘false’ statement, not the second (which seems inconsistent - hence my suggestion that it’s a bug).

If there are multiple actions within a single event they are separated by a semicolon. So if I want to include such an event within a ternary operator structure, it seems sort of half possible (for the false part, not the true). I thought perhaps putting brackets around the two actions might help but it doesn’t seem to.

OK, perhaps I should put it a different way. I have a button which, depending on a condition (the value in a record or a variable etc.) will trigger different events. These events each have two actions (they could have more). Is this possible with Wappler? If so, how can it be done?

Come to think of it, I’ve done this sort of thing before, with muliple buttons. But as far as I can see, it should be possible with a single button and a ternary operator. It almost is, but for a ‘inconsistency’ or bug in the way the ternary operator works in Wappler.

Of course, because it is considered AFTER the action before it (ternary operator) as it’s added after it.
Once again, you cannot just put random ; in random places inside the ternary operator.

If I understand you right you just need two ternary operators divided by ;

dmx-on:click="condition ? actionTrue : actionFalse; condition ? actionTrue2 : actionFalse2"

And the condition is the same in both :slight_smile:

try to use parentheses to indicate what is part of the ternary operator and what not

See:

and:

I imagined parentheses might be needed and I tried it with them, eg (arranged to make it easier to read):

dmx-on:click=“theCondition? (
defaultAction1() ; defaultAction2()
)
:
(
alternativeAction1() ; alternativeAction2()
)”

As I mentioned, the false part works (with or without parentheses), but I can’t get the first part to.
(To be more precise, using the above code doesn’t just ‘not work’ as expected - it causes a javascript error. If the true part has a single action, rather than two, it works fine).

Thanks George. I assume that once something has been assigned to Patrick, there is no need for more details - and if it’s a bug, it will be fixed.

The reason I raised this issue is not because of anything I’m doing in Wappler. I’m rewriting parts of a very large system (not web-based), which I would really like to be able to do in Wappler one day. When working on this system, I often wonder how the same thing could be done in Wappler. Testing multiple conditions when a button is clicked (for example) is something I do hundreds, if not thousands of times in this system. It wasn’t as easy in Wappler as I had hoped - but lots of things are easier in Wappler (and I would much rather be using Wappler).

try

dmx-on:click="theCondition? (defaultAction1() + defaultAction2()) : (alternativeAction1() + alternativeAction2())"
1 Like

I wasn’t in my office today and have just had a chance to try this - and it works! That’s great. Thanks Patrick.