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;
}
}