Skip to content

Instantly share code, notes, and snippets.

@ChristianMenzinger
Last active June 16, 2017 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianMenzinger/616a9a85b37d9a0070c24b59ea6612b5 to your computer and use it in GitHub Desktop.
Save ChristianMenzinger/616a9a85b37d9a0070c24b59ea6612b5 to your computer and use it in GitHub Desktop.
<aura:application extends="force:slds">
<aura:attribute name="listData" type="String[]" default="['First', 'Second', 'Third']" />
<!-- HERE comes what is propoesed by performance best practices: only one click-event listener on the outer list -->
<ul onclick="{!c.onListClick}">
<aura:iteration items="{!v.listData}" var="data" indexVar="index">
<li data-itemindex="{!index}">
<lightning:icon iconName="utility:chat" size="medium" title="title" />
Data: {!data}
</li>
</aura:iteration>
</ul>
</aura:application>
({
onListClick: function(component, event, helper) {
if (event.target === event.currentTarget) return;
//as I only have on event listener I have to step through the containment hierarchy
//to get the li-node where my data tag lives I am interested to
var el = event.target;
while (el && (!el.dataset || !el.dataset.itemindex)) {
el = el.parentElement;
}
if (el) {
console.log("Clicked item has index " + el.dataset.itemindex);
}
}
})
@ChristianMenzinger
Copy link
Author

NOTE that with API v39 this code works as expected, with API v40 it only works when clicking the text and does not when clicking the icon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment