function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siva SakthiSiva 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
 
Michał Zadrużyński 2Michał Zadrużyński 2
I'm not sure if thats what are trying to do, but if you want to pass value to child  component and view sum in parent you don't need to use app events. Simple pass array to child by {!v.arrvals} then operate on it in child. On parent make <aura:handler name="change" value="{!v.arrvals}" action="{!c.sumVal}"/> 
and in this function component.set("v.totalValue", x) where x is sum you counted
 
Siva SakthiSiva Sakthi
Given an array  values by user [10, 20, 30, 40], how can I find the sum of its elements? (In this case, the sum would be 100.)  I got the users entered values in  var txtval = event.getParam("resval");  I want to store into new array and sum it. I tried to push the entered values but in looping i am getting undefined  values..   How to achieve this any sample codes.....  [2,3,7,5,4]  sum 21 like that have to display

     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