3

I have following code which displays a list of Users in the system. It is showing the list of Users. By default the first option I see is "Select an Option" and I want the first User in the list to be defaulted. Is it possible?

<lightning:combobox name="assignedTo" label="Assigned To" 
           aura:id="inputSelectedUser" options="{!v.userList}" required="true" 
           onchange="{!c.setOutputVariables}" />

I am getting the result I want using following code. But, combobox looks way better than select

<lightning:select aura:id="inputSelectedUser" name="assignedTo" 
           label="Assigned To" onchange="{!c.setOutputVariables}">
    <aura:iteration items="{!v.userList}" var="aUser">
        <option value="{!aUser.value}">{!aUser.label}</option>
    </aura:iteration>
</lightning:select>
2

You can use the value attribute in this case to default to any item in your list.

If you refer to the example in the documentation as below:

<lightning:combobox aura:id="selectItem" name="status" label="Status"
                  placeholder="Choose Status"
                  value="new"
                  onchange="{!c.handleOptionSelected}"
                  options="{!v.statusOptions}"/>

placeholder does not have any impact here as long as value is provided. So once you provide the value attribute, you can always default it to any required value in your list.


So in your case, you can have an attribute declared which gets the first item from your list and then use that there. Something as below:

<aura:attribute name="defaultVal" type="String" default=""/>
<lightning:combobox name="assignedTo" label="Assigned To" 
                  value="{!v.defaultVal}"
                  aura:id="inputSelectedUser" options="{!v.userList}" required="true" 
                  onchange="{!c.setOutputVariables}" />
  • Thank you very much. It worked! I just had to put in a hack in my helper to populate the "{!v.defaultVal}" but that is the price I am willing to pay to make this work the way I want. – Json Bourne Shell Sep 12 '18 at 1:52
  • That's right. You just need to populate that value upfront to be able to use it. Good thing is that at least there's a way to use the value attribute. – Jayant Das Sep 12 '18 at 1:54

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.