[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

need to show United states as the first value in drop down

Hi Guys

I have a drop down menu which shows all the countries from a dataBean. What I need is to make the United States at the top. Could anyone please help me on how I can acheive this in the most efficient way. Any help will be greatly appreciated.

here is my code

<td id="<c:out value="WC_${prefix}_AddressEntryForm_TableCell_20"/>">
            
            <select class="logon select" id="<c:out value="WC_AddressEntryForm_FormInput_${prefix}country_1"/>" name="<c:out value="${prefix}country"/>" onchange="javascript:loadStatesUI(this.options[this.selectedIndex].value,<c:out value="${formName}"/>, '<c:out value="${prefix}"/>')" >
                  <c:forEach var="country" items="${countryBean.countries}">
                        <option value="<c:out value="${country.code}"/>"
                              <c:if test="${country.code eq paramSource.country || country.displayName eq paramSource.country}">
                                    selected="selected"
                              </c:if>
                        ><c:out value="${country.displayName}"/></option>
                  </c:forEach>
            </select>
      </td>
0
huzefaq
Asked:
huzefaq
  • 3
3 Solutions
 
contactkarthiCommented:
iterate the list first and see if unitedstates is there in the list if it is there add it. then add all the other countries.

or you can manipulate the bean at serverside and have US as the first vvalue
0
 
b0lsc0ttCommented:
huzefaq,

I am not a Java or Jscript expert but the most efficient way is to have option use the selected attribute.  It won't matter where in the list it appears it will be selected (or show in the select list) by default.  Of course that may not be something you wish to allow (US selected as default) but is a common way to do what you want.

To do this you would test the values you write as you loop.  If the value is US then you add the 'selected="selected"' attribute.  For example the resulting html would be  ...

<option value="US" selected="selected">US</option>

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
huzefaqAuthor Commented:
Thanks Guys

I actually tried adding the code  between teh options but it gives an error. Would you guys know anything about this error
   <c:if test="${country.code == "USA"}">
            selected="selected"
    </c:if>

ERROR
----------------
Unable to locate tag attribute info for tag attribute USA

ENTIRE SELECT
---------------------
<select class="logon select" id="<c:out value="WC_AddressEntryForm_FormInput_${prefix}country_1"/>" name="<c:out value="${prefix}country"/>" onchange="javascript:loadStatesUI(this.options[this.selectedIndex].value,<c:out value="${formName}"/>, '<c:out value="${prefix}"/>')" >
                  <c:forEach var="country" items="${countryBean.countries}">
                        <option value="<c:out value="${country.code}"/>"
                              <c:if test="${country.code == "USA"}">
                                    selected="selected"
                              </c:if>
                              <c:if test="${country.code eq paramSource.country || country.displayName eq paramSource.country}">
                                    selected="selected"
                              </c:if>
                        ><c:out value="${country.displayName}"/></option>
                  </c:forEach>
            </select>
------------------------------------------
0
 
b0lsc0ttCommented:
Remember Jscript is new to me but do you need to escape the double quotes or use other delimiters for USA?

<c:if test="${country.code == "USA"}">

In many languages the use of the double quote in front of USA would end the one that started after test=.  A JScript expert will probably be more help on this though.  I just don't know the specifics of the language to say for sure. :)

Let me know how that works or if you have a question.  One comment, you might want to add a test to make sure there is no conflict between the new code to select USA and the next part that preselects an option based on "paramSource.country."

bol
0
 
b0lsc0ttCommented:
I'm glad I could help.  Thanks for the grade, the points and the fun question.

bol
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now