0

I have nested list in my lightning component and now I want to refer records of list using indexVar. Has anyone done this before?

<aura:iteration items="{!v.records}" var="row" indexVar="rowCnt">

Like `{!row[rowCnt]}?`

</aura:iteration>
3
  • 2
    May you add some example code, why you need this?
    – itsmebasti
    Commented Oct 10, 2017 at 15:49
  • Also, share the records attribute type. Commented Oct 10, 2017 at 15:57
  • Why do you want to this? Commented Oct 10, 2017 at 16:10

1 Answer 1

4
<aura:iteration items="{!v.records}" var="row" indexVar="rowCnt">
    {!row}
</aura:iteration>

will do the trick. Getting the value dynamically would look like this, {!v.records[rowCnt]} but it doesn't work in lightning.

Failed to save undefined: expecting a positive integer, found 'rowCnt'

You only are allowed to use static integers like: {!v.records[0]}

In case you need the index in your controller, you could use a regular html element like a div and pass the Index with a data-* attribute, unfortunately this won't work with any lightning component, only pure html.

<div onclick="{!c.doStuff}" data-index="{!rowCnt}">

doStuff : function(cmp, evt, helper) {
    var index = evt.currentTarget.dataset.index;
}

You must log in to answer this question.

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