0

I am having a problem where a lightning select component definitely has a value other than the first item selected, but in the js helper method when I am calling

var temp = component.find('dropdown_id').get('v.value');
console.log(temp);

it is returning the value of the first in the select, rather than the one that is actually selected.

When my component initially loads, it only has one item in the set,

<option text="Select a field" value="INITIIAL"/>

I made the value "INITIAL" to make it obvious. There is an <aura:iteration> portion which is tied to an <aura:attribute> and the selected item is set based on an additional <aura:attribute>. These are dynamically loaded after the page is initially loaded. It works correctly because the rendered dropdown actually has the right value selected in it once the data loads. However, calling .get('v.value') is returning "INITIAL". It's not an issue of time, because for testing purposes, I can have added an arbitrary amount of delay between when the data loads and when I try to call .get('v.value') by using a test button I added to the page.

I believe what is happening is that for some reason in this particular circumstance, Lightning does not know that it needs to refresh its understanding of the state of the component.

Is there a way to manually tell Lightning to synchronize things? For example, in angular, you can manually call $apply() to synchronize things. You are generally supposed to avoid doing this, but there are certain circumstances where it is necessary.

1 Answer 1

0

You could try to handle selected value by listening event onchange rather value attribute:

<lightning:select ... onchange="{v.handleSelectValueChange}">
...
handleSelectValueChange: function(component, event, helper) {
    console.log('... selected value:', event.getParam('value'));
}
2
  • Thank you. I wonder if that would have worked given that the selected value is being changed via databinding: <aura:iteration items="{!v.listOfSomething}" var="item"> <option text="{!item.label}" value="{!item.value}" selected="{!item.selected}"/> </aura:iteration> However, I solved my problem by setting various aura:attribute values in a different order (Hmm I don't know how to format things as well in a comment reply)
    – Andy
    Commented May 14, 2020 at 15:18
  • 1
    According to documentation: The onchange event is triggered only when a user selects a value on the dropdown list with a mouse click, which is expected behavior of the HTML select element. Programmatic changes to the value attribute don't trigger this event, even though that change propagates to the select element. To handle this event, provide a change handler for value. so your approach more stable in this particular case
    – Vladimir
    Commented May 14, 2020 at 15:56

You must log in to answer this question.

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