0

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);
  • Where are you pushing a new record? The snippet you include here appears to be the initial declaration. – David Reed Dec 19 '18 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. – Sumchans Dec 19 '18 at 1:37
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);
  • Wonderful, thanks David, had been a long day, was not thinking right. thanks again. – Sumchans Dec 19 '18 at 1:43

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.