Virginia Norman Virginia Norman - 7 months ago 24
Java Question

Empty Top Option in a Struts 2 Iteration Select

Is there any way to add an Empty Top Select option in the following:

<select id="OrderNameIssueId" name="OrderWork.OrderNameIssueId" style="width: auto">
<s:iterator value="OrderWork.ClientOrderNameIssues" status="rowIdx" id="ClientIssueOption">
<option value='<s:property value="#ClientIssueOption.OrderNameIssue.OrderNameIssueId"/>' <s:if test="OrderWork.OrderNameIssueId != null && (OrderWork.OrderNameIssueId == #ClientIssueOption.OrderNameIssue.OrderNameIssueId)">selected</s:if><s:else></s:else>>
<s:property value="#ClientIssueOption.OrderNameIssue.issueDescription"/>
<s:if test="#ClientIssueOption.ApprovedOption == true">&nbsp;|&nbsp;Approved</s:if>
<s:else>
<s:if test="#ClientIssueOption.RequireFix == true">&nbsp;|&nbsp;Requires Fix</s:if>
<s:else>&#9;|&nbsp;Unapproved</s:else>
</s:else>
&nbsp;|&nbsp;Format:&nbsp;<s:property value="#ClientIssueOption.OrderNameIssue.issueHint"/>
</option>
</s:iterator>
</select>


It is on a JSP page that uses Struts 2.
The select itself works, however we need to have an option that will show up on the page like so:

<option value="0">--</option>

Answer

You should be able to add this below the "select" and above the "s:iterator". You will also want to specify when selected.

<select id="OrderNameIssueId" name="OrderWork.OrderNameIssueId" style="width: auto">
    <s:if test="OrderWork.OrderNameIssueId == null || OrderWork.OrderNameIssueId == 0">
        <option value="0" selected>--Please select an option--</option>
    </s:if>
    <s:else>
        <option value="0">--Please select an option--</option>
    </s:else>
    <s:iterator value="OrderWork.ClientOrderNameIssues" status="rowIdx" id="ClientIssueOption">
       <option value='<s:property value="#ClientIssueOption.OrderNameIssue.OrderNameIssueId"/>' <s:if test="OrderWork.OrderNameIssueId != null && (OrderWork.OrderNameIssueId == #ClientIssueOption.OrderNameIssue.OrderNameIssueId)">selected</s:if><s:else></s:else>>
       <s:property value="#ClientIssueOption.OrderNameIssue.issueDescription"/>
       <s:if test="#ClientIssueOption.ApprovedOption == true">&nbsp;|&nbsp;Approved</s:if>
       <s:else>
           <s:if test="#ClientIssueOption.RequireFix == true">&nbsp;|&nbsp;Requires Fix</s:if>
           <s:else>&#9;|&nbsp;Unapproved</s:else>
       </s:else>
       &nbsp;|&nbsp;Format:&nbsp;<s:property value="#ClientIssueOption.OrderNameIssue.issueHint"/>
       </option>
   </s:iterator>
</select>