I have a requirement to create a Lightning component that contains a ui:inputselect tag which will automatically fetch out its options from a custom pick list of an Object. How can this be achieved?
1 Answer
You should create schema for the picklist and call this method from component using aura init. this will fetch picklist value on loading the component.
public static List<String> getLeadStatus(){
List<String> options = new List<String>();
Schema.DescribeFieldResult fieldResult = lead.status.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for (Schema.PicklistEntry f: ple) {
options.add(f.getLabel());
}
return options;}
Java script controller code -
doInit : function(component, event, helper) {
var action = component.get("c.getLeadStatus");
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);
}
For picklist -
<ui:inputSelect label="Status" class="dynamic" aura:id="InputSelectDynamic" value="{!v.leadObj.Status}" required="true"/>
-
-
-
Thank you so much @Ajay , it was very helpful for me to understand the flow. Commented Aug 2, 2016 at 4:46
-
Any chance the dynamic could work for more than one picklist ? I am getting the data successfully from apex but .set("v.options", opts); is the same and it overwrites the previous Picklist. Commented Nov 2, 2016 at 14:43
-
@Ajay: Could you please add clarity on how the "options" attribute is defined? Is it as <aura:attribute name=options type=[]/> ?? Commented Aug 7, 2017 at 11:07