3

I wish to check a condition inside an aura:iteration. The condition should be dynamically set according to the current item in the iteration.

I'm trying to achieve the following :

<aura:if isTrue="{!c.myTestFunction(parameter)}">
    <!-- ... -->
</aura:if>

With the following in controller :

myTestFunction : function(parameter) { ... }

Of course this is not working. So my question is : is there any workaround to achieve this ? I would like not to update my iteration objects with a new property "toDisplay" that would require many updates.

2 Answers 2

1

It could be very useful sometimes, but you can't call methods like that (you can only invoke them with events) and you can't pass parameters to them. So in your case, I would recommend creating a component that you render in your iteration:

<aura:iteration items="{!v.myObjects}" var="obj">
    <c:yourComponent value="{!obj}" param="{!v.parameter}" />
</aura:iteration>

And your component could look like this:

<aura:component>
    <aura:attribute name="value" type="Object">
    <aura:attribute name="param" type="String">
    <aura:attribute name="isRendered" type="Boolean" access="private">

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <aura:if isTrue="{!v.isRendered}">
        <!-- ... -->
    </aura:if>
</component>


doInit: function(cmp) {
    var rendered = //doSomethingWithParam
    cmp.set("v.isRendered", rendered);
}
4
  • repeatedly calling init in aura iteration looks like bad idea. Might slow down the page performance. Commented Nov 29, 2017 at 19:34
  • 2
    @PranayJaiswal It's JavaScript, so unless you've thousands upon thousands of items, the entire execution time is likely to be insignificant.
    – sfdcfox
    Commented Nov 29, 2017 at 19:39
  • I can confirm what sfdcfox mentioned, we are already doing this eg. with a large grid, having no issues.
    – itsmebasti
    Commented Nov 29, 2017 at 19:42
  • Thank you for your answer I'll do a wrapper component then! :)
    – thiout_p
    Commented Nov 29, 2017 at 20:02
2

You have only two choices here:

<aura:if isTrue="{!currentItem.display}">

Or

<aura:if isTrue="{!eq(currentItem.display, 'some value')}">

If you really need a function, you do need an attribute you can bind to, so you'll want to initialize the value as part of the attribute. You can't call a method with parameters, and you can't call a method period except in situations where it's allowed (primarily the action parameter of most components that have it, as well as event handlers like onclick or pressed), and even those don't accept parameters.

You must log in to answer this question.

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