You need to sign in to do that
Don't have an account?
Siva Sakthi
how to push iteration dynamic values into array and sum the pushed values in lightning component
Hi,
I have try to sum the iteration text box values from child component into parent component. I can able to get the child values from child component via application & component events. Pls guide me how to push those child values in new array and sum it.
Parent Component:
----------------------------
<aura:component >
<aura:attribute name="arrvals" type="integer[]" default="1,2,3,4,5"/>
<aura:registerEvent name="sum" type="c:SumAppEvent"/>
<aura:handler name="sumres" event="c:SumCompEvent" action="{!c.sumVal}"/>
<aura:attribute name="myResults" type="integer[]" />
<aura:iteration var="num" items="{!v.arrvals}" aura:id="ipv" indexVar="index">
<c:myListComponent />
</aura:iteration>
<ui:button aura:id="addbtn" label="Calculate" press="{!c.calculate}" /><br/>
Sum of the Value : <ui:outputNumber aura:id="totalValue" value="{!v.totalValue}" />
</aura:component>
Parent Controller:
------------------------
sumVal : function(component, event, helper) {
var txtval = event.getParam("resval");
console.log('got value from child',txtval);
var val = component.getEvent("sum");
val.setParams({"myResults" : txtval});
console.log('val******',val);
for (var total = 0, i = 0; i <txtval; i++) {
val.push({ value: txtval });
//total += parseInt(txtval);
}
console.log('sum +++++++',val);
},
calculate : function(component, event, helper) {
$A.get("e.c:SumAppEvent").fire();
} ,
})
Advance Thanks
Siva
I have try to sum the iteration text box values from child component into parent component. I can able to get the child values from child component via application & component events. Pls guide me how to push those child values in new array and sum it.
Parent Component:
----------------------------
<aura:component >
<aura:attribute name="arrvals" type="integer[]" default="1,2,3,4,5"/>
<aura:registerEvent name="sum" type="c:SumAppEvent"/>
<aura:handler name="sumres" event="c:SumCompEvent" action="{!c.sumVal}"/>
<aura:attribute name="myResults" type="integer[]" />
<aura:iteration var="num" items="{!v.arrvals}" aura:id="ipv" indexVar="index">
<c:myListComponent />
</aura:iteration>
<ui:button aura:id="addbtn" label="Calculate" press="{!c.calculate}" /><br/>
Sum of the Value : <ui:outputNumber aura:id="totalValue" value="{!v.totalValue}" />
</aura:component>
Parent Controller:
------------------------
sumVal : function(component, event, helper) {
var txtval = event.getParam("resval");
console.log('got value from child',txtval);
var val = component.getEvent("sum");
val.setParams({"myResults" : txtval});
console.log('val******',val);
for (var total = 0, i = 0; i <txtval; i++) {
val.push({ value: txtval });
//total += parseInt(txtval);
}
console.log('sum +++++++',val);
},
calculate : function(component, event, helper) {
$A.get("e.c:SumAppEvent").fire();
} ,
})
Advance Thanks
Siva
and in this function component.set("v.totalValue", x) where x is sum you counted
sumVal : function(component, event, helper) {
var txtval = event.getParam("resval");
console.log('got value from child',txtval);
var myResults =[];
myResults.push(txtval);
for (var total = 0,i = 0; i <myResults.length; i++) {
total += myResults[i];
}
component.set('v.totalValue',total);
},
Thanks
Siva