[SalesForce] Aura : Input Select , Drop Down

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.

Best Answer

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 an 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 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;
    }
}
Related Topic