liferayliferay-6liferay-themealloy-uiliferay-aui

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


Solution

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