You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Marcus Andersson <ma...@active-tv.com> on 2002/12/06 13:51:29 UTC

Multiple choice select

Hi all

I'm not very good at Struts yet so maybe my question is a bit stupid.

I have two collections: one with all elements from some collection and one
collection wich is a subset of the first collection (the selected elements).
What is the easiest way to render a select box with all the elements from
the first and with all elements from the second collection selected? Is
there an easier way than just looping through the elements in the first
collection and check for existence in the second (looked on the
options-element in Struts but don't really get it)? I also wonder how an
ActionForm looks when having a select box that is multiple-enabled.

/Marcus
__________________________________________________ 
Marcus Andersson 
Project Leader and Application Developer 
ActiveTV AB 
Östgötagatan 19 
Box 124 
581 02 LINKOPING 
Phone: +46 13 461 80 28 
Mobile: +46(0)709-29 96 68 
Fax: +46 13 461 80 99 
E-mail: marcus.andersson@active-tv.com 
Web: www.active-tv.com <www.active-tv.com>  
__________________________________________________ 



Re: Multiple choice select

Posted by Kris Schneider <kr...@dotech.com>.
Marcus,

Not at all stupid, the whole select/option thing can get a bit hairy.

Let's say your form allows the selection of ice cream flavors (I'm hungry this
morning). If you want to allow the selection of multiple flavors, your action
form would have methods like:

public String[] getFlavors()
public void setFlavors(String[] flavors)

to define a flavors property. This property represents the set of currently
selected flavors. To represent the set of all possible flavors, you should look
into creating a collection of org.apache.struts.util.LabelValueBean instances.
So, for each possible flavor, create an instance of LabelValueBean, add it to
the collection, then save the collection in the appropriate scope:

allFlavors.add(new LabelValueBean("Chocolate", "choc"));
allFlavors.add(new LabelValueBean("Vanilla", "van"));
allFlavors.add(new LabelValueBean("Strawberry", "straw"));
// lots more please
context.setAttribute("allFlavors", allFlavors);

In your JSP you'd have something like:

<html:form action="/do/feedMe">
  ...
  <html:select property="flavors" multiple="true">
    <html:optionsCollection name="allFlavors"/>
  </html:select>
  ...
</html:form>

If the action form's flavors property is set to { "choc", "straw" }, here's how
the HTML would get rendered:

<form name="feedMeForm" action="/appContext/do/feedMe" method="post">
  ...
  <select name="flavors" multiple="multiple">
    <option value="choc" selected="selected">Chocolate</option>
    <option value="van">Vanilla</option>
    <option value="straw" selected="selected">Strawberry</option>
  </select>
  ...
<form>

The important thing to recognize is the link between the action form's property
and the value property of the LabelValueBean instances. When the
html:optionsCollection tag renders an HTML option tag, it grabs the value
property of the associated LabelValueBean instance and compares it to the form's
property to see if it matches. If it does, the selected attribute gets rendered.

Hm, that was sort of long-winded (now I'm really hungry), but hopefully it
helped. Also make sure to read through:

http://jakarta.apache.org/struts/userGuide/struts-html.html#select
http://jakarta.apache.org/struts/userGuide/struts-html.html#option
http://jakarta.apache.org/struts/userGuide/struts-html.html#options
http://jakarta.apache.org/struts/userGuide/struts-html.html#optionsCollection

Quoting Marcus Andersson <ma...@active-tv.com>:

> Hi all
> 
> I'm not very good at Struts yet so maybe my question is a bit stupid.
> 
> I have two collections: one with all elements from some collection and one
> collection wich is a subset of the first collection (the selected
> elements).
> What is the easiest way to render a select box with all the elements from
> the first and with all elements from the second collection selected? Is
> there an easier way than just looping through the elements in the first
> collection and check for existence in the second (looked on the
> options-element in Struts but don't really get it)? I also wonder how an
> ActionForm looks when having a select box that is multiple-enabled.
> 
> /Marcus
> __________________________________________________ 
> Marcus Andersson 
> Project Leader and Application Developer 
> ActiveTV AB 
> Östgötagatan 19 
> Box 124 
> 581 02 LINKOPING 
> Phone: +46 13 461 80 28 
> Mobile: +46(0)709-29 96 68 
> Fax: +46 13 461 80 99 
> E-mail: marcus.andersson@active-tv.com 
> Web: www.active-tv.com <www.active-tv.com>  
> __________________________________________________ 
> 
> 
> 


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>