+ Start a Discussion
Max PaqMax Paq 

Get all the values from an aura:iteration

Hello everyone,

Is there a way to get all the values from an aura:iteration element? Both output and input value? I have the following code in my component and I would like to get all the values in the inputNumber field, the {!subset} value and match them together.

<aura:iteration items="{!v.spaceSubsets}" var="subset">
      <p>{!subset}  <ui:inputNumber aura:id="detailRating" label='Rating'/></p>
</aura:iteration>

The end goal here would be to click on a button and have the component insert multiple records into the database.

Bonus point if you could help me figure out how to only create record when a value is entered for one of the iteration.

 

Thank you all.

gdm1986gdm1986

Facing the same issue - I need a data structure to save inputs inside an aura iteration. The aura:id's are dynamic so I can distinguish any input, but I cannot use the indexVar property to directly reference a varibale in an array in order to bind it directly to the lightning:input

<aura:iteration items="{!v.positions}" var="pos" indexVar="index">
    <c:lightningPicklist aura:id="{!pos.Id}" required="false" name="actionPick" commaSeparatedValues="Sale Shares, Sale $, Sell All, Buy Shares, Buy $" blankopt="true"/>
    <lightning:input aura:id="{!'quant'+pos.Id}" type="number"/>
    <lightning:input aura:id="{!'lim'+pos.Id}" type="number" />
    <c:lightningPicklist aura:id="{!'pick'+pos.Id}" required="false" name="actionPick" commaSeparatedValues="Day,GTC" blankopt="true" /></aura:iteration>
Ideally I could do 
value="{!v.someArray[index]}"

Has anyone found a good solution for this use case?
Guilherme OliveiraGuilherme Oliveira
@Gidiem:
Theoretically, you could, on your inputs, put value="{!pos.quantity}", for example, and your array is dinamically changed (even if your "position" object doesn't have this attribute originally). Then, on your JS controller, you can iterate through "positions" and get "quantity" for each member of the array (if "Position" is an sObject, you can even use it to bind back to the  Apex controller).

That said, @Max, is there a reason you're not using attributes on your "subset" records? If you make that into a JS object, with two attributes (e.g.: "output" and "input"), you can iterate on your controller and match those attributes as you like (maybe make a list that you populate depending on this "matching", that you send back to your Apex controller), like so:
<aura:iteration items="{!v.spaceSubsets}" var="subset">
      <p>{!subset.output}  <ui:inputNumber aura:id="detailRating" label='Rating' value="{!subset.input}"/></p>
</aura:iteration>
And, on the JS controller:
var subsets = component.get("v.spaceSubsets");
var matchingSubsets;
for(var i = 0; i < subsets.length; i++){
    var subset = subsets[i];
    if(subset.output == subset.input{
        matchingSubsets.add(subset);
    }
}
//call apex controller passing matchingSubsets