Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the selected value in aui:select box

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!!

like image 633
Vinod Avatar asked Apr 02 '15 15:04

Vinod


2 Answers

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>
like image 83
Parkash Kumar Avatar answered Oct 07 '22 08:10

Parkash Kumar


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>
like image 3
admirm Avatar answered Oct 07 '22 10:10

admirm