6

I am trying to create a lightning:inputField using $A.createComponents. The issue I have is that I am not able to add an onchange event. The field Level__c is a picklist field.

When I use the following code in the component the onchange event works fine:

<lightning:inputField fieldName="Level__c" onchange="{!c.reload}"/>

However, when I use the following code within $A.createComponents I get an error message ,Cannot read property 'indexOf' of undefined'.

Component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="contract" type="Contract__c" default="{ 'sobjectType': 'Contract__c' }" access="public"/>
 <aura:handler name="init" value="{!this}" action="{!c.init}" /> 
<lightning:recordEditForm recordId="{!v.recordId}" objectApiName="Contract__c">
    {!v.body}
</lightning:recordEditForm>

Controller

({
init: function(cmp, event, helper){
    helper.createComponents(cmp);   

},

reload: function(cmp, event, helper){
    helper.createComponents(cmp);        
}})

Helper

({
createComponents: function(cmp){        
    console.log(cmp.getReference("c.reload"));
    $A.createComponent("lightning:inputField", {"fieldName"  : "Level__c", "aura:id": "Level__c", "onchange":cmp.getReference("c.reload")},
    function(newCmp, status, errorMessage){
        if (status === "SUCCESS") {
            var body = cmp.get("v.body");                
            body.push(newCmp);
            cmp.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);
        }
    });  
},})
5
  • 2
    What is the error message. Please edit your question and add relevant information.
    – Raul
    Commented Feb 8, 2018 at 14:17
  • Action failed: c:LoadContractEdit$controller$init [Cannot read property 'indexOf' of undefined]
    – Chris K.
    Commented Feb 8, 2018 at 15:15
  • Error seems to be with indexOf property in init method, check once. Otherwise share some code with which we can reproduce the issue?
    – Raul
    Commented Feb 8, 2018 at 20:03
  • I updated the code.
    – Chris K.
    Commented Feb 9, 2018 at 7:14
  • It's not indexOf in @ChrisK. code. It's something internal. I get it also. Commented Mar 22, 2018 at 22:24

1 Answer 1

1

I found a workaround. I just put the onchange event on the parent div as shown below. However, it would be nice to be able to assign it directly to lightning:inputField. I assume there is a bug in the code.

var changeOrderType = component.getReference("c.changeOrderType");
    newComponents.push(["aura:html", {"tag": "div", "HTMLAttributes": {"class": "slds-form-element__control", "onchange": changeOrderType}}]);      
    newComponents.push(["lightning:inputField", {"fieldName"  : "Contract_type__c", "aura:id": "input_contract_type__c", "value": contract.Contract_type__c}]); 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .