M Alok
M Alok

Reputation: 1081

same option gets selected two times in drop down , how this can be stopped?

i am trying to build a project in which a user submits a ticket and can edit it later after submitting . on create ticket page there is a drop down menu for selecting priority of the ticket with options - highest,high,medium,low . there is a table in database for priority options . when user click on a edit link corresponding to a ticket , user is forwarded to an edit page where different fields of the ticket are shown populated . for showing the selected option for priority menu i am doing this , as follows

<select name="priority">
 <c:forEach var="priority"items="${priorityList }">
    <c:if test="${tempTicket.priorityId == priority.priorityId}">
        <option value="${priority.priorityId }" selected="selected">${priority.priorityName}</option>
    </c:if>
   <option value="${priority.priorityId }">${priority.priorityName </option>
 </c:forEach>
</select>

In this the selected option is shown two times in the drop down on the edit page , how can this be stopped ? is there any other way that can fullfill the same purpose ? i have tried using two <c:if> .

Upvotes: 0

Views: 251

Answers (3)

JB Nizet
JB Nizet

Reputation: 691913

First solution:

<c:if test="${tempTicket.priorityId != priority.priorityId}">
    <option value="${priority.priorityId }">${priority.priorityName }</option>
</c:if>

Second solution:

<c:choose>
    <c:when test="${tempTicket.priorityId != priority.priorityId}>
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

Third (and best) solution:

<option value="${priority.priorityId }" 
        <c:if test="${tempTicket.priorityId == priority.priorityId}">selected="selected"</c:if>>
    ${priority.priorityName}
</option>

Upvotes: 4

Vartlok
Vartlok

Reputation: 2589

Try this

<option value="${priority.priorityId }" 
${tempTicket.priorityId == priority.priorityId? "selected" : ""} 
>${priority.priorityName}</option>

Upvotes: 1

drgPP
drgPP

Reputation: 936

Else implementation in jstl is

choose tags

<c:choose>
  <c:when test="cond1">
    Condition 1
  </c:when>
  <c:otherwise>
    Condition 2 (if condition 1 is false)
  </c:otherwise>
</c:choose>

Upvotes: 1

Related Questions