1

I am trying to push data in the controller variable to eventually push it onto the component attribute variable. As of the now the record gets pushed but it clears the previous record that was in there. Please advise!

Here is variable declaration in the component:

<aura:attribute name="closedWons" type="Opportunity[]"/>

Table with iteration:

<tbody>
    <aura:iteration items="{!v.closedWons}" var="cw">
         <tr style="color:#163e85"> 
             <td>
                <div class="slds-text-heading_x-small slds-align_absolute-center">{! cw.Name }</div>
                    </td>
                    <td>
                        <div class="slds-text-heading_x-small slds-align_absolute-center">{! cw.sumchans__Owner_Name__c }</div>
                    </td>
                    <td>
                        <div class="slds-text-heading_x-small slds-align_absolute-center">{! cw.sumchans__Owner_Manager__c }</div>
                    </td>
                </tr>
            </aura:iteration>
        </tbody>

Variable declaration in the controller:

var oppsToClosedWons = [];
oppsToClosedWons.unshift(oppData);
component.set("v.closedWons", oppsToClosedWons);
2
  • Where are you pushing a new record? The snippet you include here appears to be the initial declaration. Commented Dec 19, 2018 at 1:28
  • At the bottom you see the actual push code. Pushing onto oppsToClosedWons array, then from there to the v.closedWons component attribute. Commented Dec 19, 2018 at 1:37

1 Answer 1

2

Your code isn't pushing a new value onto an existing array; you start from an empty array and push onto that.

var oppsToClosedWons = [];

Here you create the variable as an empty list.

oppsToClosedWons.unshift(oppData);
component.set("v.closedWons", oppsToClosedWons);

The previous value of v.closedWons is ignored here - any values you've added to the initially-empty oppsToClosedWons replace the current value of the component attribute.

If your intention is to add values to an existing component attribute value, you need to obtain the current value first, add a value onto that, and then re-set the component attribute:

var oppsToClosedWons = component.get("v.closedWons");
oppsToClosedWons.unshift(oppData);
component.set("v.closedWons", oppsToClosedWons);
1
  • Wonderful, thanks David, had been a long day, was not thinking right. thanks again. Commented Dec 19, 2018 at 1:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.