I believe that what you really are wanting to do is use the "<OPTGROUP>" tag within 
HTML's "<select>" tag. This allows you to create
a two level indented list of categories and subcategories in a select list.

Unfortunately Struts does not directly support this. I asked about this a couple of 
months ago - and got no response at all. I
imagine the lack of support is for two reasons:

1) The data structure to support it would be "slightly" more complicated - a 
collection of category object, each category object
would need a few properties and a collection of (sub)category objects.

2) The browser support for OPTGROUP has been spotty until recently. Under Windows it 
was not supported in MSIE until version 5.5
(IIRC) and with Netscape support started in version 6 (again, IIRC). However the 
<OPTGROUP> is supposed to degrade gracefully in
older browsers and still be usable - not so sure about that.

If the browser support is not an issue, then you can still OPTGROUP with struts - IF 
you are willing to do a little scripting (or
even better create your own custom tag). We do and are very pleased with the results, 
although we have had to make some minor
adjustments to support different browsers on different platforms.

Here are a couple of references that should be useful.

  http://www.w3.org/TR/REC-html40/interact/forms.html#edef-OPTGROUP
  http://tagsnstyles.host.sk/elements/optgroup.html

Here is a snippet of the code that I use in my JSP to create the indented select lists 
(I did change variable names to be a little
more general and removed a few complexities). I ended up using a combination of JSTL 
and JSP scriptlets (not pretty, but it works).
If we decide to use this on a more widespread basis,a custom taglib is definitely in 
order. I've included two versions, the first
only works with Windows and MSIE 6.0, Netscape 7.x and Mozilla Firebird (may work with 
other browsers, but not tested). The second
also works with Macintosh MSIE 5.1 - turns out that Mac MSIE 5.1 has quite a few 
differences - you would think both being written by
MS, they might ... I know, the problem is I "thought" - :(. Have not had a chance to 
test under Macintosh Safari, maybe it will not
be so picky. Anyway, this is not elegant at all, but it works and the effect is quite 
nice.

SNIPPET - Supports Windows only (see above)

  <hmtl:select property="subCategoryId" indexed="true" styleId="newsubCategoryId">
    <optgroup label="">
      <option value="" label="">&nbsp;</option>
    </optgroup>
    <c:set var="prevCategory" value=""/>
    <c:forEach items="${Collection_SubCategory}" var="subCat">
      <c:set var="currentCategory" value="${subCat.subject_name}"/>
      <c:set var="currentSubCategory" value="${subCat.name}"/>
      <c:if test="${prevCategory != currentCategory}">
        <c:if test="${!empty prevCategory}">
        </optgroup>
        </c:if>
      </c:if>
      <c:if test="${prevCategory != currentCategory && !empty currentSubCategory}">
          <optgroup label="<c:out value="${currentCategory}"/>">
      </c:if>
      <c:if test="${currentSubCategory != ''}">
      <option value="<c:out value="${subCat.id}"/>" label="<c:out 
value="${currentSubCategory}"/>"
        <c:if test="${subCat.id == categoryForm.subCategoryId}">
          selected="true"
        </c:if>
      ><c:out value="${currentSubCategory}"/></option>
      </c:if>
      <c:set var="prevCategory" value="${subCat.subject_name}"/>
    </c:forEach>
    </optgroup>
  </hmtl:select>


SNIPPET - Supports Windows and Mac (see above)

  <hmtl:select property="subCategoryId" indexed="true" styleId="newsubCategoryId">
  <% if ( request.getHeader("user-agent").toLowerCase().indexOf("win") > -1 ) { %>
    <optgroup label="">
      <option value="" label="">&nbsp;</option>
    </optgroup>
  <% } else { %>
      <option value="">&nbsp;</option>
  <% } %>
    <c:set var="prevCategory" value=""/>
    <c:forEach items="${Collection_SubCategory}" var="subCat">
      <c:set var="currentCategory" value="${subCat.subject_name}"/>
      <c:set var="currentSubCategory" value="${subCat.name}"/>
  <% if ( request.getHeader("user-agent").toLowerCase().indexOf("win") > -1 ) { %>
      <c:if test="${prevCategory != currentCategory}">
        <c:if test="${!empty prevCategory}">
        </optgroup>
        </c:if>
      </c:if>
      <c:if test="${prevCategory != currentCategory && !empty currentSubCategory}">
          <optgroup label="<c:out value="${currentCategory}"/>">
      </c:if>
  <% } else { %>
      <c:if test="${prevCategory != currentCategory && !empty currentSubCategory}">
        <option value="<c:out value="${currentCategory}"/>" readonly="true" 
style="font-weight: bold;">
          <c:out value="${currentCategory}"/>
        </option>
      </c:if>
  <% } %>
      <c:if test="${currentSubCategory != ''}">
  <% if ( request.getHeader("user-agent").toLowerCase().indexOf("win") > -1 ) { %>
      <option value="<c:out value="${subCat.id}"/>" label="<c:out 
value="${currentSubCategory}"/>"
        <c:if test="${subCat.id == categoryForm.subCategoryId}">
          selected="true"
        </c:if>
      ><c:out value="${currentSubCategory}"/></option>
  <% } else { %>
      <option value="<c:out value="${subCat.id}"/>"
        <c:if test="${subCat.id == categoryForm.subCategoryId}">
          selected="true"
        </c:if>
      >
    <% if ( request.getHeader("user-agent").toLowerCase().indexOf("msie 5.0; 
mac_powerpc") > -1 ) { %>
        &nbsp;&nbsp;&nbsp;&nbsp;
    <% } else { %>
        ....
    <% } %>
      <c:out value="${currentCategory}"/> - <c:out 
value="${currentSubCategory}"/></option>
  <% } %>
      </c:if>
      <c:set var="prevCategory" value="${subCat.subject_name}"/>
    </c:forEach>
  <% if ( request.getHeader("user-agent").toLowerCase().indexOf("win") > -1 ) { %>
    </optgroup>
  <% } %>
  </hmtl:select>


Hope this helps - Richard



-----Original Message-----
From: Sethi, Mandeep [mailto:[EMAIL PROTECTED]
Sent: Friday, October 17, 2003 7:39 AM
To: 'Struts Users Mailing List'
Subject: <HTML:SELECT> giving headaches




Hi Everyone

I'm working on displaying a collection using <HTML:SELECT>.
I'm our case we have a collection of value objects in session scope ,so for
e.g
in our case we have a collection of Category in session scope ,Which further
has
a collection of subCategory and I need to display them in a select list in
way like

Category name1
...subCategory name1
...subCategory name2

Category name2
...subCategory name1
...subCategory name2


class Category()
{
        String name;
        String id;
        Collection subCategory;

}

Class SubCategory{
String subName;
String subID;
}

So far I'm successfull with first part that is displaying Category name with
their id as value ,my code is

<html:select property="availableList" size="18" multiple="true">
<html:options collection="availableFieldList"
 property="lookupType"
 labelProperty="lookupTypeLabel"/>
 </html:select>


where availableFieldList is the collection of Category ,

Can anyone suggest few ideas for my second step of displaying subcategories
too in required fashion,

Thank You


****************************************************************************
This email may contain confidential material.
If you were not an intended recipient,
Please notify the sender and delete all copies.
We may monitor email to and from our network.
****************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to