I have a table with dynamic number of rows. But all of the has the same structure: summary row and details row. The details row implemented as separated lightning component. Also I have a "+" sign on the summary row that should expand details row and call a method of child component.
Currently I'm seeking for required details component by looping the array of details components returned by component.find('') method. But I assume it's not very efficient. I've checked the developer guide and found out that I can get a component by it's global id Finding component by Id
But when I try to call a method of that component I get an error (something like "there is no such method").
So do I need to do something more to convert aura$html to actual component or it's not possible at all?
Is it a bad practise to instantiate child components in the <aura:iteration>
tag and it's better to create them dynamically using JS?
2 Answers
I'm working on this right now. It appears that aura:id
can't take variables, so calling <c:MyComp aura:id="{!index}-childComp" />
will fail.
Instead I'm doing:
<aura:iteration items="{!v.insights}" var="insight" indexVar="indx">
<c:MInsightCell aura:id="minsight-cell" insight="{!insight}" index="{!indx}" maxCount="{!v.insights.length}"/>
</aura:iteration>
(where index
is an attribute on my child component).
To then access the component I do:
var next_index = swiper.activeIndex + 1;
var next_id = next_index+'-minsight-cell';
debugger;
var slides = component.find("minsight-cell");
var next_slide = slides[next_index];
next_slide.preload(next_id);
(I'm using the Swiper library which keeps of track of which index it's on...)
Moral: aura:id
is limited, but required if you want to use methods on components.
Do you have an <aura:method/>
in your component markup? You'll need to expose the method using this tag in order to call it.
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cmp_methods.htm
Creating child components inside of <aura:iteration/>
is fine. If you're going to modify any values in the collection that created those components you'll want to do it from the component that contains the collection. Don't allow the child components to modify themselves.
-
1Yes, I have
<aura:method>
in the markup and I can successfully call it when I find component usingcomponent.find()
method in controller. But I can't call the same method if I get component by$A.getComponent()
method.– stealsCommented Feb 19, 2016 at 8:52 -
-
component.getGlobalId();
when your component is created.