function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vinnie Bacon 20Vinnie Bacon 20 

How do you simply set an attribute based on aura:if?

I'm trying to do something I assume is pretty simple but am getting stumped on how to do it in Lightning.  MY pseudocode is:

<aura:attribute name="printedSpecialRow" type="Boolean" default="false" />

<aura:iteration items="{!v.myList}" var="anItem">
    <aura:if isTrue="{!!v.printedSpecialRow}">
        <tr>Print Special Row</tr>
        <!-- Now set the value of printedSpecialRow to true -->
    </aura:if>
</aura:iteration>

I have simple controller method that sets the printedSpecialRow value to true.  That's easy enough.  But how do I call this method from within the aura:if block to set the value so the next iteration will not call the aura:if block.

This has gotta be easy right.  :)  Thanks!
Maharajan CMaharajan C
Hi Vinnle,

You can use the below way to achieve this:

<aura:attribute name="printedSpecialRow" type="Integer"  default="1"/>   

<aura:iteration items="{!v.myList}" var="anItem" indexVar="idx">
<aura:if isTrue="{!lessthan(idx,v.printedSpecialRow)}"> 
<tr>Print Special Row</tr>
</aura:if>
</aura:iteration>

You can also use the below method:
https://salesforce.stackexchange.com/questions/205704/using-auraif-inside-the-auraiteration
https://salesforce.stackexchange.com/questions/138760/is-there-a-salesforce-solution-to-filter-or-limit-aura-iteration

Thanks,
Maharajan.C
Vinnie Bacon 20Vinnie Bacon 20
Thanks!  Knowing about the index is helpful.  However, I can't rely on that.  My situation is that I may have say 20 items in my list.  The first three or so may be of type A and require that group heading (i.e. special row).  Then the next few are of type B and I want that group heading above them.  So, except for the first one I don't know what index number I will want to put the headings into.

I decided that the best approach was to have each group be its own component.  I can grab all 20 or so items in one query.  Then create the five sub-lists I need from those.  For each sub-list I create the component which has a header and its list of items.

I remember hearing that modularization of components was the best way to use Lightning.  Time to put that into practice. :)