I am building a menu component.
One menu-item is a sub-menu with list of opportunities, this list is created with aura:iteration
over a list of opportunities.
When selecting a menu or a sub-menu, I need to make it look selected (change css attribute). For this to happen, I need to find this menu item in my controller.
If I add id
attribute to my item markup and use the event attribute on my controller (event.currentTarget.id
), I can catch the item itself and change it's style, this works:
MARKUP
<aura:iteration items="{!v.opportunities}" var="opp">
<li class="slds-dropdown__item" role="presentation">
<a href="javascript:void(0);" role="menuitem" tabindex="-1" id="{!opp.Id}" onclick="{!c.openItemFromMenu}">
<span class="slds-truncate" title="{!opp.Name}">{!opp.Name}</span>
</a>
</li>
</aura:iteration>
CONTROLLER
openItemFromMenu: function(component, event, helper) {
// get the id
var oppId = event.currentTarget.id;
// choose it (select menu)
var selectedItemNew = component.find(oppId);
selectedItemNew.style = "background: rgb(216, 237, 255);"
}
PROBLEM
when I try to change the menu selected not from the click of the item (event that is triggered from another page) - I have added an event handler to change the menu item, and I send the opportunity id through that event. Then (after adding aura:id
to my item, I am trying to find this menu item of this oppId with component.find
in my controller - this does not work:
CHANGED MARKUP
add:
<aura:handler name="eventSelectMenuItem" event="c:myEvent" action="{!c.openItemFromMenu}" />
change:
<a href="javascript:void(0);" role="menuitem" tabindex="-1" id="{!opp.Id}" onclick="{!c.openItemFromMenu}" aura:id="{!opp.Id}">
CHANGED CONTROLLER
openItemFromMenu: function(component, event, helper) {
// get the id from the event
var oppId = event.getParam("recordId");
// choose it (select menu)
var selectedItemNew = component.find(oppId);
selectedItemNew.style = "background: rgb(216, 237, 255);"
}
QUESTION
Does component.find
for aura:id
within an aura:iteration
suppose to work?
If not - is there a workaround?
If it does suppose to work - what am I doing wrong?