[SalesForce] How retrive the value from ui::inputSelect

I have a Lightning component with a list of the Account from Salesforce:

    <ui:inputSelect label="Account" class="form-control" aura:id="selectAccount"  onSelectChange="{!c.onSelectChange}">
        <aura:iteration items="{!v.accounts}" var="account">
            <ui:inputSelectOption value="{!account.Id}" label="{!account.Name}" />
        </aura:iteration>
    </ui:inputSelect>

It works right. How to retrive the Id of the selected account option?
I'm expecting on the onSelectChange callback to see the params, but with:

onSelectChange: function(component, event, helper) {
    console.log(event);
    console.log(event.getParams());
}

I get this output:

app.js:14666 Event {source: Component, $eventDef$: EventDef, $eventDispatcher$: undefined, $eventName$: "change", $params$: Object…}
app.js:14667 Object {}

While I'm expecting to see at least the name of the Account..

Any advice?

Best Answer

I see a few potential errors in the code. onSelectChange should be change, and value should be text.

Please try this:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="contacts" type="Contact[]"/>

<ui:inputSelect aura:id="o" label="Contacts" change="{!c.onSelectChange}">
    <aura:iteration items="{!v.contacts}" var="contact">
        <ui:inputSelectOption text="{!contact.Id}" label="{!contact.Name}" />
   </aura:iteration>
</ui:inputSelect>

And then you can use the component.find().get() pattern to retrieve the Id.

onSelectChange : function(component, event, helper) {
    var selected = component.find("o").get("v.value");
    console.log(selected);
},

doInit : function(component, event, helper) {
    // Your own implementation for loading records
    helper.getContacts(component); 
}

Here are a few docs related to ui:inputSelect:

Related Topic