7

Component Code:

<aura:component controller="TestController" implements="forceCommunity:availableForAllPageTypes">   
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
  <aura:attribute name="country" type="Contact" default="{ sobjectType: 'Contact' }" />


  <ui:inputSelect label = "country" aura:id="InputSelectDynamic">
    <aura:iteration items="{!v.country.Country__c}" var="CountryName">
           <ui:inputSelectOption text="{!CountryName}" label="{!CountryName}"/>
    </aura:iteration>
          
  </ui:inputSelect> 
</aura:compomnent>

Controller Code

({
  doInit: function(component, evt, helper) {
    
    var action = component.get("c.getAccount");
    action.setCallback(this, function(a) {
     component.set("v.country", a.getReturnValue());
     
    });
      
    $A.enqueueAction(action); 
  },
  


})

Apex Controller

public class TestController{ 
  @AuraEnabled 
  public static Account getAccount() {
    return [select Country__c from Contact]; 
  }
}

Question : I am not getting any record in the drop down , can any one tell me what is wrong with the code. my controller is having simple SQL select query and that is working fine.

5
  • 1
    post your "TestController" apex class too please. Also you iterate on contact list, I see that you are iterating on country of the contact(assuming it is a string, you would get an error)
    – Rao
    Oct 30, 2015 at 16:05
  • I'd suggest posting the Apex code as well. It is conceivable that something is going wrong there. Finally, are you getting any errors in the developer tools? Have you checked the profile of the community user has access to the Apex class, and to Contact?
    – pchittum
    Oct 30, 2015 at 16:06
  • well spotted @Rao I suspect it is iterating on the field instead of the attribute that is the problem. You should post an answer.
    – pchittum
    Oct 30, 2015 at 16:10
  • @AuraEnabled public static Account getAccount() { return [select LastName__c from Account ]; }}
    – Nikx
    Oct 30, 2015 at 16:33
  • <aura:attribute name="account" type="Account" default="{ sobjectType: 'Account' }"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <force:inputField value="{!v.account.LastName__c}" /> <br/> <br/> <ui:inputSelect label = "country" aura:id="InputSelectDynamic"> <aura:iteration items="{!v.account.LastName__c}" var="Name"> <ui:inputSelectOption text="{!Name}" label="{!Name}"/> </aura:iteration> </ui:inputSelect>
    – Nikx
    Oct 30, 2015 at 16:34

1 Answer 1

3

To just display picklist country__c related to the contact on the UI

Approach 1 using force:inputfield

Component:

<aura:component controller="testconaura">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:attribute name="contact"  type="Contact" default="{ sobjectType: 'Contact' }" />
    <force:inputField value="{!v.contact.LeadSource}">
</aura:component>

Controller:

({
    doInit : function(component, event, helper) {
    var action = component.get("c.getcontact");
    action.setCallback(this, function(a) {
     component.set("v.contact", a.getReturnValue());
    });
    $A.enqueueAction(action); 
  }
})

Apex Class:

public class testconaura {
  @AuraEnabled 
  public static contact getcontact() {
    return [select id,name,LeadSource from Contact limit 1]; 
  }
}

Renderer:

There is a bug with picklist when using force:inputField for which a workaround is provided here

Lightning Components: why force:inputField picklist select renders disabled?

({
    render : function(cmp, helper) {
    var element = this.superRender();
    element[0].children[0].removeAttribute('disabled');
    return element;
}
})

Approach 2: populating pick list values dynamically through apex controller:

COPY PASTE THE EXACT CODE AND CHANGE THE FOLLOWING LINE IN THE APEX CLASS TO GET THE DESIRED PICKLIST

Schema.DescribeFieldResult fieldResult = contact.Leadsource.getDescribe();

Component:

<aura:component controller="dcontroller">
  <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
    <ui:inputSelect label="Lead source pickval: " class="dynamic" aura:id="InputSelectDynamic"/>
</aura:component>

JS Controller:

({
    doInit : function(component, event, helper) {
        var action = component.get("c.getpickval");
        var inputsel = component.find("InputSelectDynamic");
        var opts=[];
        action.setCallback(this, function(a) {
            for(var i=0;i< a.getReturnValue().length;i++){
                opts.push({"class": "optionClass", label: a.getReturnValue()[i], value: a.getReturnValue()[i]});
            }
            inputsel.set("v.options", opts);

        });
        $A.enqueueAction(action); 
        }
})

Apex class:

public class dcontroller {
    @AuraEnabled
    public static List<String> getpickval() {
        List<String> options = new List<String>();
    
        Schema.DescribeFieldResult fieldResult = contact.Leadsource.getDescribe();
    
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    
        for (Schema.PicklistEntry f: ple) {
            options.add(f.getLabel());
        }       
        return options;
    }
}
12
  • Hello Rao , in the <aura:iteration items="{!v.country} if i use v.country - i get failed to initialize component JS error So i used items = !v.CountryName.Country__c
    – Nikx
    Oct 30, 2015 at 16:36
  • i changed the code to iterate name field having multiple records - but drop down is blank - posted the component code and controller code above
    – Nikx
    Oct 30, 2015 at 16:43
  • @nikx is country__c a picklist? are you trying to just put the country picklist related to the contact on the UI?
    – Rao
    Oct 30, 2015 at 16:58
  • I am ok with either of it - 1. I tried with picklist in input field but facing issue with making it enable as by default it is disabled 2. Then I tried with inputselect - where I am fetching account records and trying to populate dynamically. I am looking for a drop down code in lightning component which can be generated dynamically using controller
    – Nikx
    Oct 30, 2015 at 17:01
  • My original code was based on country__c as a picklist - but that would not work in inputselect. So i changed it to fetching records and populating in inputselect. If you have code for generating dropdown dynamically you can share.
    – Nikx
    Oct 30, 2015 at 17:05

You must log in to answer this question.

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