0

How to show Salesforce data into a dynamically created standard component. I'm dynamically creating ui:inputText field which is used to display Custom Object value in UI. How can I display field value in this ui:inputText field.

Component:

<aura:component>
    <lightning:button label="Click Me" onclick="{!c.clickMe}" />
    {!v.body}
</aura:component>

Controller:

clickMe : function(com,eve,helper){
    helper.compCreate(com,eve,helper);
}

Helper:

compMap:{
    "1":[["ui:inputText",{"aura:id":"ar0"}],["ui:inputText",{"aura:id":"ar1"}]]
},
compCreate:function(com,eve,helper){
    $A.createComponents(this.compMap["1"],function(newComp,status,errorMessage){
        if (status === "SUCCESS") {
            component.set("v.body", newComp);
        }
    }
}

Here compMap will contains screen number as Key and UI component as the list of component. How I can specify the Salesforce value in my compMap only.

  • did you try "1":[["ui:inputText",{"aura:id":"ar0"},{"value" ="Hello"}],["ui:inputText",{"aura:id":"ar1"}]]. instead of hello you can pass a variable which will hold your salesforce value – RedDevil Jun 9 '17 at 18:03
  • that means If I will have 15 fields then I need to create 15 variable – salesforce Developer Jun 9 '17 at 18:06
0

That can be achieved by defining a attribute for every field (the work around for this is to create an array of the fields in a map since component.getReference() will only work with an attribute I haven't been able to find for subtype of an attribute). There were some issues with the binding of the values in the compMap definition.

The component will comprise of the button to make the call to controller and an attribute of the object type the form will capture, for example, here we are using an Account object.

<aura:component >
    <lightning:button label="Click Me" onclick="{!c.clickMe}" /> 
    <aura:attribute name="Name" type="String"/>
    <aura:attribute name="CompanyName" type="String"/>
    {!v.body}
</aura:component>

The controller will be the same

clickMe : function(component,event,helper){
    helper.compCreate(component,event);
}

I have restructured the helper to include the compMap inside the function so it is possible to access the component. I have also included the label attribute for clarity.

({
    compCreate:function(component,event){
        var compMap = {
                "1":[["ui:inputText",{"aura:id":"ar0","value":component.getReference("v.Name"),"label":"Name"}],["ui:inputText",{"aura:id":"ar1","value":component.getReference("v.CompanyName"),"label":"Company Name"}]]
        }
        $A.createComponents(compMap["1"],function(newComp,status,errorMessage){
            if (status === "SUCCESS") {
                component.set("v.body", newComp);
            }
        });
         console.log(compMap["1"]);
    }
})

If you want to avoid defining an attribute for every single field, you could create a field mapping which will capture any change in value manually (will update when I implement). One reference that I found very useful How to use fieldsets with Lightning?

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.