4

We are now starting moving our application to Lightning by re-writing it. We are facing some problems during the process and the one which is now blocking us is the following.

We have one component called timesheetGrid.cmp which has an attribute called currentTimesheetLines I'm correctly loading and render data from it on the controller. But I need to pass this attribute from this one timesheetGridRenderTSEColumnSummaryCell passing it like that.

<aura:component description="timesheetGrid" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="LgtTimesheetCtrl">
//...
  <aura:attribute name="currentTimesheetLines" type="TimesheetCode__c[]" 
//...
<aura:iteration items="{!v.currentTimesheetLines}" var="timesheetLine" indexVar="i">
                {!timesheetLine.Code__r.Description__c}
</aura:iteration>
<c:timesheetGridRenderTSEColumnSummaryCell timesheetLines="{!v.currentTimesheetLines}"/>
</aura:component>

/>

The problem is even when I can iterate through and render data from it. On the timesheetGridRenderTSEColumnSummaryCell controller when I read it I can get it, it's always an empty array.

Can someone help with this issue?

This happens just with this type of "array of objects" params, with any other doesn't.

1

Arrays have been always tricky, I found a long route out by Stringifying the response and pass it to the second component controller and the deserializing into an object again.

Eg:

App:

<aura:application >
<c:passarraycomp1 ></c:passarraycomp1>
</aura:application>

comp 1:

<aura:component controller="passtoarraycontroller" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="arraytopass" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <c:passarraycomp2 arrayofaccounts="{!v.arraytopass}"/>
</aura:component>

comp 1 controller:

({
    doInit : function(component, event, helper) {
        var action = component.get("c.serverEcho");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log('@@@@'+response.getReturnValue());
                component.set("v.arraytopass",JSON.stringify(response.getReturnValue()));
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);

    }
})

comp2:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="arrayofaccounts" type="String"/>
    <lightning:button label="fetchvalueofsecondcomponent" onclick="{!c.getarrayofaccs}"/>
</aura:component>

comp2 controller:

({
    getarrayofaccs : function(component, event, helper) {
        console.log('#####'+ component.get("v.arrayofaccounts"));
        //use JSON.deserialize to convert this back to an array.
    }
})

Server side controller:

public class passtoarraycontroller {

    @AuraEnabled 
    public static list<account> serverEcho(){
        return [select id,name from account limit 10];
    }
}

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.