This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes Need  help using 2 select lists in the same form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Need  help using 2 select lists in the same form" Watch "Need  help using 2 select lists in the same form" New topic
Author

Need help using 2 select lists in the same form

Laura Matei
Greenhorn

Joined: May 11, 2005
Posts: 1
Hello! I need some help. I have two dropdown lists on the same page. If I select an option in the first one I want the other one to change correspondingly.

For example: I have a list of event domains (culture, entertainment) and for each domain I have a list of categories (culture with theatre, opera,films and entertainment with parties, shows, concerts). I defined the event domain list in the class EventForm.java as an arrayList. I don't know how to define the lists for the event categories such that when choosing a domain in the form, automaticaly to display the corresponding categories. I give you the example for my first option list as I call it in the JSP:

<html:select name="workingEvntForm" property="event.chosenEventDomain>
<html ptions name="workingEventForm" property="eventDomains" />
</html:select>

in the EventForm.java

private static final List EVENT_DOMAIN_LIST = new ArrayList();
//private static final List EVENT_CATEGORY_LIST = new ArrayList();

/* Private Fields */

private Event event;
private List eventDomainList;

/* Static Initializer */

static {
EVENT_DOMAIN_LIST.add("CULTURE");
EVENT_DOMAIN_LIST.add("ENTERTAINMENT");
EVENT_DOMAIN_LIST.add("SPORT");
EVENT_DOMAIN_LIST.add("COMERCE");
}
/* Constructors */

public EventForm() {
this.event = new Event();
this.eventCityList = EVENT_CITY_LIST;
this.eventDomainList = EVENT_DOMAIN_LIST;
this.eventCategoryList= EVENT_CATEGORY_LIST;
}

/* JavaBeans Properties */

public Event getEvent() { return event; }
public void setEvent(Event event) { this.event = event; }
public List getEventDomains() { return eventDomainList; }

//-------------------------------

Please if you have an example it would help a lot! Thank you!
Laura
Nimchi Yung
Ranch Hand

Joined: Jan 27, 2004
Posts: 71
One approach is using Javascripts. Then you don't need to post the
request back to server when user clicks on any choice box.

--Nimchi
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If when a user chooses an option in the first select box, the second select box can only be one of 2 or 3 different select boxes, I'd render all the possible select boxes with style="visibility:hidden" and then just use javaScript to display the correct one when the user makes a choice in the first select box.

However, if there are many possible choices for the second select box, I'd recommend submitting the form with the onchange event of the first select. Then have your action class format the proper select box for the second option and redisplay the page.


Merrill
Consultant, Sima Solutions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help using 2 select lists in the same form
 
Similar Threads
Pre-select collection of options (drop-downs)
populating a combo box
Form Params not passed properly
Problem setting form property in action
How to populate html:select options in struts