0

I am trying to set the options of a lightning combobox component dynamically by using component.set('v.options', myOptions) as was the case for the now deprecated ui:inputSelect but it seems not to be working. Is this no longer supported for the combobox?

Markup

<aura:attribute name="myVariable" type="Map" default="{val1:'', val2:''}" />
<aura:attribute name="myOptions" type="List" default="[{label: 'Option 1', value: 'option_1'}, {label: 'Option 2', value: 'option_2'}]" />

<lightning:combobox aura:id="required_input"
    required="true" placeholder="-- Select --" />

Controller

let inputCmp = component.find('required_input');
if (inputCmp) {
    inputCmp.set('v.label', 'testLabel');
    inputCmp.set('v.value', component.getReference('v.myVariable.val2'));
    inputCmp.set('v.options', component.getReference('v.myOptions'));
}

The options are not being displayed in the component.

1
  • can you add the relevant code here? Commented Jan 21, 2020 at 8:44

1 Answer 1

1

You can set options values by using options property of combobox

<aura:component>
    <aura:attribute name="options" type="List" default="[
    {'label': 'New', 'value': 'new'},
    {'label': 'In Progress', 'value': 'inProgress'},
    {'label': 'Finished', 'value': 'finished'},
    ]"/>

    <lightning:combobox name="progress" label="Status" value="inProgress" placeholder="Select Progress" options="{! v.options }" onchange="{! c.handleChange }"/>

</aura:component>
5
  • I know of that way of setting the options. I was just inquiring if we can also set them dynamically by setting the options attribute of the combobox directly from the controller.
    – user73893
    Commented Jan 21, 2020 at 10:12
  • 1
    init: function (cmp) { var items = []; for (var i = 0; i < 500; i++) { var item = { "label": i + " Option", "value": i.toString() }; items.push(item); } cmp.set("v.options", items); } use above to set option from controller Commented Jan 21, 2020 at 10:17
  • What if I want to use another attribute besides options to populate the combobox?
    – user73893
    Commented Jan 21, 2020 at 10:21
  • @user73893 you can just specify a different attribute name, default values, etc. Commented Jan 28, 2020 at 16:15
  • 1
    @GibralterTop it is the specifying part that is proving to be the issue. I do not want to do so statically in the markup. I want to do so dynamically in the controller. Say I created another attribute called myOptions2 and wanted to use it as the combobox options under certain conditions. I would just have to change this line inputCmp.set('v.options', component.getReference('v.myOptions')); to this line inputCmp.set('v.options', component.getReference('v.myOptions2')); But this isn't working. Do you see my point?
    – user73893
    Commented Jan 29, 2020 at 4:13

You must log in to answer this question.

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