How to set the selected value in aui:select box
Asked Answered
T

3

6

I have form with select box in my user form. I also need to update the form in edit mode. I am able to get selected value in edit mode. But I am unable to set the selected value in the edit mode.

Here I am able to get the selected value from db. <%=user.getTitle() %> Now how can I set the selected value in my select box.

<aui:select name="title">
    <aui:option label="Dr" value="dr" />
    <aui:option label="Mr" value="mr" />
    <aui:option label="Mrs" value="mrs" />
    <aui:option label="Ms" value="ms" />
</aui:select>

Example I aset the selected value for input field like this,

<aui:input name="emailAddress" value=""></aui:input>

Any suggestions please!!

Titular answered 2/4, 2015 at 15:56 Comment(1)
simply, put selected attribute as true for aui:option tag, by comparing saved value to option value.Gisela
C
11

You can set selected value as:

Hard-coded Options:

<aui:select name="title">
    <aui:option label="Dr" value="dr" selected="<%=user.getTitle().equals("dr") %>" />
    <aui:option label="Mr" value="mr" selected="<%=user.getTitle().equals("mr") %>" />
    <aui:option label="Mrs" value="mrs" selected="<%=user.getTitle().equals("mrs") %>" />
    <aui:option label="Ms" value="ms" selected="<%=user.getTitle().equals("ms") %>" />
</aui:select>

Dynamic Options:

<aui:select name="title">
    <%for(int i = 0; i < listOfOptions.size(); i++){ 
        Object option = listOfOptions.get(i);
        boolean selected = false;
        if(user.getTitle().equals(option.getTitle())){
            selected = true;
        } %>
        <aui:option label=<%=option.getTitle() %> value="<%=option.getValue() %>" selected=<%=selected %> />
    <%} %>
</aui:select>
Cuspidor answered 3/4, 2015 at 6:20 Comment(0)
R
5

A cleaner way to do it:

<aui:select name="title">
  <c:forEach items="${listOfOptions}" var="currOption">
    <option value="${currOption}"<c:if test="${currOption == title}"> selected="selected"</c:if>> 
       ${currOption}
     </option>
  </c:forEach>
</aui:select>
Rote answered 3/5, 2016 at 11:51 Comment(0)
C
1

You can use ternary operator

    <c:forEach var="currOption" items="${listOfOptions}">
        <aui:option value="${currOption}" label="${currOption}" selected="${currOption==selOption?true:false}" />
    </c:forEach>
Contamination answered 21/6, 2018 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.