4

Everything I have read online so far suggests that one must manually declare picklist (option) values when creating a lightning:select component in an Lightning application. For us, this is less than ideal.

Before I throw in the towel on this idea, I thought I'd ask Stack Exchange. Is it possible yet to have a lightning:select component automatically populate with options declared in the bound field's picklist values?

1
  • I think you're looking for lightning:inputField instead?
    – sfdcfox
    May 31, 2018 at 18:19

2 Answers 2

5

The basic method today is to get the values from the server, then render the values with $A.createComponents.

doInit: function(component, event, helper) {
  var action = component.get("c.init");
  action.setParams({ ... });
  action.setCallback(this, function(result) {
    var options = [];
    result.getReturnValue().values.forEach(
      function(value) {
        options.push(['option', { label: value, value: value }]);
      });
    $A.createComponents(
      options,
      function(components) {
        component.find("mypicklist").set("v.options", components);
      });
    });
  $A.enqueueAction(action);
}

If that's too complicated, then you might consider using lightning:inputField, which is designed to work with lightning:recordEditForm.

There's no way to get lightning:select automatically populate with the options when bound to a field.

1
3

Here is how I populate lightning:select with picklist values:

1) create a server-side method that returns list of all picklist values available

2) call this method when lightning component is initialized

3) iterate through list of picklist options using <aura:iteration> inside <lightning:select> and add each of them as <option>

Don't have exact examples behind, but this approach works just fine.

You must log in to answer this question.

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