Activity Forums Salesforce® Discussions how to use $A.create Component for creating an existing component dynamically in Salesforce?

  • Deepak

    Member
    September 3, 2019 at 6:19 am

    Create a component dynamically in your client-side JavaScript code by using the $A.createComponent() method. To create multiple components, use $A.createComponents().$A.createComponent Syntax is looking as shown below.

    1
    $A.createComponent(String type, Object attributes, function callback)
    type—The type of component to create; for example, “ui:button”.
    attributes—A map of attributes for the component, including the local Id (aura:id).
    callback(cmp, status, errorMessage)—The callback to invoke after the component is created
    In this example, The client-side controller calls $A.createComponent() to create a ui:inputText with a local ID. The function(newInp, status, errorMessage) callback appends the inputText to the body of c:createComponent.<aura:component >
    <lightning:button variant="brand" label="Create Component" onclick="{! c.handleComponent }" />
    {!v.body}
    </aura:component>

    ({
    handleComponent : function(component, event, helper) {
    $A.createComponent(
    "ui:inputText",
    {
    "aura:id": "inpId",
    "labelClass":"slds-form-element__label",
    "placeholder":"Enter Some Text",
    "label": "Enter some text",
    "class": "slds-input"
    },
    function(newInp, status, errorMessage){
    if (status === "SUCCESS") {
    var body = component.get("v.body");
    body.push(newInp);
    component.set("v.body", body);
    }
    else if (status === "INCOMPLETE") {
    console.log("No response from server or client is offline.")
    }
    else if (status === "ERROR") {
    console.log("Error: " + errorMessage);
    }
    }
    );
    },
    })

    • This reply was modified 5 years, 2 months ago by  Deepak.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos