[SalesForce] first value as a default in Drop Down List

i need to default the first value of the drop down list and pass it to my controller.

Below is my code. Please suggest what wrong i am doing.

Component:

 <aura:attribute name="pltfrmGrpOptions" type="string[]"/>
    <div class="container">
        <form aura:id="frm1">
            <fieldset>
            <ui:inputSelect aura:id="ProductPlatform" class="form-control" label="Product Platform" change="{!c.onSelectChange}" required="true"> 
                <aura:iteration items="{!v.pltfrmGrpOptions}" var="level">
                    <ui:inputSelectOption value="{!level}" label="{!level}" text="{!level}" />
                </aura:iteration>
            </ui:inputSelect> 
            ....
        </fieldset>
        </form>
    </div>

i need to mark the first value in the drop down list as the default value. Please suggest.
Currently, if i do not select any value from drop down, my controller receives an undefined value.

Best Answer

As Tushar said you shouldn't pass any blank option value in the controller. Still if you want to set a dropdown value as default value in your Lightning component you can do this as below. <aura:iteration has a attribute indexVar which provides the position of the dropdown starting from 0. You can use this with <aura:if to set value="true" for any dropdown value to make it default. In your case it will be 0 as the first one is default. Hope this helps.

Example

<aura:iteration items="1,2,3,4,5" var="item" indexVar="index">
    <aura:if isTrue="{!index ==0}" >
        <ui:inputSelectOption text="{!item}" value="true"/>
        <aura:set attribute="else">
            <ui:inputSelectOption text="{!item}"/>
        </aura:set>
    </aura:if>
</aura:iteration>

Your code will be updated as below

 <aura:attribute name="pltfrmGrpOptions" type="string[]"/>
    <div class="container">
        <form aura:id="frm1">
            <fieldset>
            <ui:inputSelect aura:id="ProductPlatform" class="form-control" label="Product Platform" change="{!c.onSelectChange}" required="true"> 
                <aura:iteration items="{!v.pltfrmGrpOptions}" var="level" indexVar="index">
                    <aura:if isTrue="{!index ==0}" >
                    <ui:inputSelectOption value="{!level}" label="{!level}" text="{!level}" value="true" />
                    <aura:set attribute="else">
                        <ui:inputSelectOption value="{!level}" label="{!level}" text="{!level}" />
                    </aura:set>
                </aura:if>
                </aura:iteration>
            </ui:inputSelect> 
            ....
        </fieldset>
        </form>
    </div>
Related Topic