0

I have a dynamic picklist in a lightning component :

<lightning:select name="selectField" label="Champ" required="true" aura:id="selectField">
                <aura:iteration items="{!v.fields}" var="field">
                    <option text="{!field.value}" value="{!field.key}"/>
                </aura:iteration>
</lightning:select>

The options are displayed correctly, but when I click an option to select it, it does not select it.

I see that there is an attribute value available for the tag lightning:select but I don't know what to put inside it, and there is an attribute selected for the tag option but I don't know which value to put in it too.

1

Looks like you are missing the "Selected" attribute, on the Option element Ex:

<option text="{!field.label}" value="{!field.value}" selected="{!field.selected}"/>
0

This works for me - I have an attribute for the selected value, and a changehandler to handle changes, which lets you access the selected value:

 <aura:attribute name="selectedValue" type="String"/>
 <aura:attribute name="accountrtvalues" type="List"/>

 <aura:handler name="change" value="{!v.selectedValue}" action="
{!c.handleChange}"/>

 <lightning:select name="mySelect" label="Select a Record Type:" aura:id="mySelect" value="{!v.selectedValue}">
 <aura:iteration items="{!v.accountrtvalues}" var="item">
 <option text="{!item.label}" value="{!item.value}" selected="{!item.selected}"/>
 </aura:iteration>
 </lightning:select>

Then in controller:

 handleChange: function (component, event, helper) {
        //Do something with the change handler
        console.log(event.getParam('value') )
 },
1
  • the op is missing the selected attribute
    – glls
    Jun 27 '18 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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