0

I have been trying to to simplify a nested <aura:if isTrue> into a single line. I thought I could wrap this with an or(statement1,statement2) but I think I'm missing something. The expected functionality works with the nested statement, the functionality is to only show option 'y' when 'x' is selected beforehand.

<aura:if isTrue="{!or(and(v.object.field__c == 'x' , item.label == 'y'), item.label != 'y')}">
      <aura:if isTrue="{!or(and(v.object.field== 'x' , item.label == 'z'), item.label != 'z')}">
                <option selected = "{!item.selected}" value="{!item.value}">{!item.label}</option>
      </aura:if>
</aura:if>

My attempt

<aura:if isTrue="{or(!or(and(v.object.field__c == 'x' , item.label == 'y'), item.label != 'y'),!or(and(v.object.field== 'x' , item.label == 'z'), item.label != 'z'))}">
                <option selected = "{!item.selected}" value="{!item.value}">{!item.label}</option>
</aura:if>

Attempt with And

 and((!or(and(v.object.field__c == 'x',item.label == 'y'), item.label != 'y')),(!or(and(v.object.field__c == 'x',item.label == 'z'), item.label != 'z')))
    

Any help would be greatly appreciated.

3
  • Given you are going from a nesting scenario to a flat scenario, the operator needs to be AND, not OR. if (a) { if (b) { x } } is equivalent to if (a AND b) { x }.
    – Phil W
    Dec 2, 2020 at 17:41
  • Also just keep in mind, there is a limitation where you can only evaluate two parameters in and and or parameters. Dec 2, 2020 at 17:52
  • I tried it with an and as I included above and still did not work as expected. Any ideas? @PhilW
    – fynmnx
    Dec 2, 2020 at 18:06

1 Answer 1

4

Personally I would use the Apex/Java/C++/C style infix OR and AND operators (|| and &&) instead of the or and and functions (the downside being that & is a special character in XML and therefore must be encoded as &).

Try something like:

<aura:if isTrue="{!((v.object.field__c == 'x' &amp;&amp; item.label == 'y') ||
                    (item.label != 'y')) &amp;&amp;
                   ((v.object.field == 'x' &amp;&amp; item.label == 'z') ||
                    (item.label != 'z'))}">
    <option selected = "{!item.selected}" value="{!item.value}">{!item.label}</option>
</aura:if>

If sticking with the "operator functions", you would need something like:

<aura:if isTrue="{!and(or(and(v.object.field__c == 'x', item.label == 'y'),
                          (item.label != 'y')),
                       or(and(v.object.field == 'x', item.label == 'z'),
                          (item.label != 'z')))}">
    <option selected = "{!item.selected}" value="{!item.value}">{!item.label}</option>
</aura:if>

This is, IMHO, much harder to read and understand (and I can't even guarantee I converted it correctly either)!

BTW, note how I have formatted to align the two sides of each operator or the two parameters for each function. It makes it easier to read and understand in general so I encourage you to adopt that sort of approach in this sort of case.

4
  • This worked perfectly. Much easier to understand then the OR and AND operators.
    – fynmnx
    Dec 2, 2020 at 18:24
  • Would you be able to show me this example written in the other way as well? Using AND and OR? @PhilW
    – fynmnx
    Dec 2, 2020 at 18:47
  • @PhilW Thanks for this. I never knew we could use Apex/Java/C++/C style infix in Aura. Is it also applicable in LWC html ? Dec 5, 2020 at 15:36
  • LWC doesn't support expressions unfortunately.
    – Phil W
    Dec 5, 2020 at 16:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .