Lightning – How to Set Parameters in Lightning Component Controller

I'm working on a lightning:select tag to display account names in the dropdown list, whenever I select an account from the list it shows all the opportunities but I want only account related opportunities for that I'm passing ID parameter to the apex controller but it's not showing anything.

Component

<aura:component controller="BookFaircls"> 
    <aura:attribute name="account" type="Account[]" />
    <aura:attribute name="opportunity" type="Opportunity[]" />
    <aura:attribute name="Id" type="ID"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <lightning:select name="selectItem" aura:id="selectItem" label="" onchange="{!c.oppRecords}">
    <aura:iteration items="{!v.account}" var="acc">
    <option value="{!acc.Id}">{!acc.Name}</option>
    </aura:iteration</ligh‌​tning:select>
    <aura:iteration items="{!v.opportunity}" var="opp"> {!opp.Name} </aura:iteration>
</aura:component>

Controller

 ({ 
        doInit : function(component, event, helper) { 
        var action = component.get("c.getAccountNames");
         action.setCallback(this, function(response){
         component.set("v.account", response.getReturnValue());
         }) 
        $A.enqueueAction(action);
         }, 

        oppRecords : function(component, helper){ 
        var value = component.get("c.getOpportunityDetails");
        value.setParams({Id :component.get("v.Id")}); value.setCallback(this,function(res){ 

        component.set("v.opportunity", res.getReturnValue()); })
        $A.enqueueAction(value);
        }
 }) 

Best Answer

As you missed the attribute value="", you can't get the selected value. So you should mention the attribute value="{!v.Id}"in lightning:select component.

<lightning:select name="selectItem" aura:id="selectItem" label="" value="{!v.Id}" onchange="{!c.oppRecords}">
    <aura:iteration items="{!v.account}" var="acc">
    <option value="{!acc.Id}">{!acc.Name}</option>
    </aura:iteration>
</lightning:select>
Related Topic