4

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
  • 2
    yes. at apex controller use describe call to get all the picklist label and value.. and return to lightning. And based on those values prepare picklist in lightning
    – Ratan Paul
    Commented Jul 28, 2016 at 7:37

1 Answer 1

13

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"/> 
8
  • But it is not showing options Commented Jul 28, 2016 at 8:41
  • Can you please post your code?? Commented Jul 28, 2016 at 10:40
  • 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=[]/> ??
    – user28452
    Commented Aug 7, 2017 at 11:07

You must log in to answer this question.

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