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?

  • 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 Jul 28 '16 at 7:37
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"/> 
  • But it is not showing options – Shobhit Saxena Jul 28 '16 at 8:41
  • Can you please post your code?? – Ajay Prakash Dubey Jul 28 '16 at 10:40
  • Thank you so much @Ajay , it was very helpful for me to understand the flow. – Shobhit Saxena Aug 2 '16 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. – Miroslav Handzhiev Nov 2 '16 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 Aug 7 '17 at 11:07

protected by Community Nov 2 '16 at 23:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.