0
<aura:attribute name="draftOptions" type="Map"/>

<lightning:select aura:id="draftOptionsAuraId" name="draftOptions" onchange="{!c.actionLoadSelectedDraft}">
    <option value="">--Load Draft--</option>
    <aura:iteration items="{!v.draftOptions}" var="option" indexVar="key">
        <option text="{!option.value}" value="{!option.key}"/> 
    </aura:iteration>
</lightning:select>

component.find('draftOptionsAuraId').set('v.value', draft.Id); changes the select value in the backend, but the UI doesn't re-render. Why?

1

It looks like your <aura:iteration> is creating <option> entries that don't match your data, which will result in the UI not showing a value properly set in the attribute to which it is bound.

<aura:iteration items="{!v.draftOptions}" var="option" indexVar="key">
    <option text="{!option.value}" value="{!option.key}"/> 
</aura:iteration>

Here, you're iterating over your options, but the values you're assigning to the text and value attributes appear to be wrong. If these are sObjects, as they seem to be from your use of Id to set the backend value, it's more likely that your <option> should look like this:

    <option text="{!option.Name}" value="{!option.Id}"/> 

More generally, the value should be one of the values that you set in the bound attribute, and vice versa.

  • I made the changes, but still no luck. Seems like it could be a time sensitive problem. The value I need the select box to show is a new value coming from the apex controller. If I hard code set the Select value to some existing value, it works, not with the new value. – Sidharth Agarwal Apr 22 '19 at 18:24
  • Would you add some additional code context to your question so that we have a complete example? – David Reed Apr 22 '19 at 18:36
1

Below solution worked for me. Thanks, everyone for your input.

Unable to set lightning:select value when using dynamically created options

0

Sorry I wanted to comment rather than answer but I don't have enough reputation yet to comment. The new value you are getting from Apex must be already in the picklist options before setting that value. If you can share here all the related code it would help to respond.

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.