This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes select another dropdown based on a selection from first dropdown using <html:select> struts tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "select another dropdown based on a selection from first dropdown using <html:select> struts tag" Watch "select another dropdown based on a selection from first dropdown using <html:select> struts tag" New topic
Author

select another dropdown based on a selection from first dropdown using <html:select> struts tag

raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
I have the below dropdowns. I need to select the second dropdown (email) when the user select an entry in first dropdown (accounts). if accountid2 is selected, email-id2 must be selected. if other, other should be selected. how to do that using onchange fuction. please note that it uses struts tag - <html:select>

<td>1. Member Account Number<span class="bodyCopy"><font color="#ff0000"> * </font></span>:
<html:select name="DataForm" property="accountNumber" styleClass="formContent"
style="width:80px" >

<option>${reDataForm.accountNumber}</option>

<htmlptions collection="<%= WorkConstants.RENewDropdowns.PACCT %>" property="value" labelProperty="label" styleClass="formContent"/>
</html:select>

<td>3. Member <br>E-mail Address:<br /> <span class="bodyCopy" ></span>
<html:select name="DataForm" property="emailAddress" style = "width:150px" styleClass="formContent">
<option>${reDataForm.emailAddress}</option>
<htmlptions collection="<%= WorkConstants.RENewDropdowns.PEMAIL %>" property="value" labelProperty="label" styleClass="formContent"/>
</html:select>
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Create an onchange method for the first select to call a javascript and do your logic there...
There is nothing to do with struts here, just simple js, try it


Have Fun with Java
little,little.. little by little makes a lot..
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
Thanks Shankar,
the only concern for me is there is no id/name that I can identify. I tried another jsp using regular <SELECT>. Since it is struts tag <html:select> which doesnt have a name/id to give. See my sample code for <SELECT> which works fine. how do you use the same if i have <html:select> tag with label and value (see my code in my first posting)

<HTML>
<HEAD>
<script language="javascript" type="text/javascript" >
<!-- hide

function selectPartner(x){
if (document.form1.Person.value != "null") {
alert(x);
document.form1.Native.options[x].selected =true

}
}

// end hide -->
</script>
</HEAD>
<body>
<form name="form1">
<select name="Person" onChange="selectPartner(document.form1.Person.options[document.form1.Person.options.selectedIndex].value)">
<option>Choose..</option>
<option value=1>Vegitable</option>
<option value=2>Fruits</option>
</select>

<select name="Native">
<option>Native is..</option>
<option value=0>Okra</option>
<option value=1>Apple</option>
</select>

</form>
</body>
</HTML>

Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Raj, there should be some relation with the two selects that you are using and you can pass that relation ID to the js and still do that
What i can see from the post is the relation is account number and use that account number on selection set the email..

Did i miss something?

raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
thanks Shankar.
I have 5 accounts and 5 emails. one to one relationship (means first account corresponds to first email). Both are seprate dropdowns. Could you please shed some more info how to do this as I'm new to javascript.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

Struts 2 has a doubleselect tag that does what you want.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Raj, Firstly use code tags, its very easy to read and looks good isnt it?...

Anyways, your select box would be like this

and js something like this



Just saw a reply, i though you were using lower versions of struts, if its struts2 please use the double select (what a mistake)
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
Joe/Shankar,

how do you use doubleselect ? can you please provide an example, based on my requirement ? appreciate your help !
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
Shankar, is that how iterate should code ? - ${iterate <%= WorkConstants.RENewDropdowns.PEMAIL %>}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

Did you look at the examples on the page I linked? Are you using Struts 2?
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
Joe, Looks like I'm using older version - Manifest-Version: 1.0. does that mean, I can not use doubleselect ?
if so, could you please let me know how to iterate thru the the list i have (list stored in a constant).
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8935
    
    9

raj mal wrote:Manifest-Version: 1.0.


That refers to the version of the manifest file format, not the Struts version.
Shankar was just giving you the rough outline of the code. We ask that you ShowSomeEffort and try to solve your own problem.
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
thanks Joe. I was trying to iterate using for loop in javascript. But I dont know how to get the values from the session constant - WorkConstants.RENewDropdowns.PACCT . Any help on this would appreciated !
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Set the list in your form to get the same in js or use AJAX to get the list from session asynchronously.

Better try it yourself first and see if you could achieve it. If yes, you would remember it for long else you can put here what you have tried and we shall see whats happening, then again you would remember it for long.
raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
thanks shankar. please look at this code.

function selectEmail(currentAccountNumber){

alert (currentAccountNumber);

for
( int i = 1; i < 6; i++)

{
if (currentAccountNumber = WorkConstants.RENewDropdowns.PACCT.getAccountId1(i) ){
//how to set email here ?

}
}
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

try something like this in JS, and again it is just an idea

raj mal
Greenhorn

Joined: Feb 11, 2011
Posts: 11
got it . thanks ! appreciate your help !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: select another dropdown based on a selection from first dropdown using <html:select> struts tag