[SalesForce] Issue passing values to a list – Lightning component

I have been developing a lightning component to add products and can't seem to get my ids pushing to my productsList. My current revelant code is as follows:

CMP

<aura:component controller="AddProductsController" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global">
<aura:attribute name="productList"  type="List"/>
<aura:attribute name="options" type="String" default="Nothing" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    <aura:handler name="change" value="{!v.cardType}" action="{!c.handleValueChange}"/>
    <aura:handler name="change" value="{!v.options}" action="{!c.handleSelect}"/>
    <aura:handler name="add" value="{!v.productList}" action="{!c.handleProduct}"/>

<div class="align-right">
              <lightning:buttonicon iconName="utility:add" size="medium" alternativeText="Add" onclick="{!c.handleProduct}"/>
          </div>
          <h1>{!v.options} Added</h1>
            List:
           <aura:iteration var="list" items="{!v.productList}">
                {!productList.value}
            </aura:iteration> 

My ids are passing into my options attribute fine.

Controller

handleProduct: function(cmp, evt){
    var Product = cmp.find("productType").get("v.value");
    cmp.set("v.options", Product);
    var PL = cmp.get("v.productList");
    PL.push(Product);
}

I think I have given all the relevant code associated with the action of passing to my list, obviously I am doing something wrong so any advice would be hugely appreciated.

Thanks

Best Answer

You want to set after push into a product List

handleProduct: function(cmp, evt){ 
var Product = cmp.find("productType").get("v.value"); cmp.set("v.options", Product);
 var PL = cmp.get("v.productList");     
 PL.push(Product); 
cmp.set("v.productList",PL);
}

And another bug in your code you iterating over list of Id that you are pushing in aura:iteration you want to refer using instance I.e) var

   <aura:iteration var="idInList" items="{!v.productList}"> 
{!idInList} 
</aura:iteration> 
Related Topic