You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rick Col <ri...@yahoo.com> on 2003/07/24 15:26:50 UTC

dependent list help!

Hi,

I can use the follwoing javascript code to dynamically
fill a dependent list (students is aa array of
arrays).

<SELECT NAME="department"
onChange="populateDependent(this.form.student,
((this.selectedIndex == -1) ? null :
students[this.selectedIndex-1]));">
<OPTION VALUE="-1">Select Team
<OPTION VALUE=1>Montreal Canadiens
<OPTION VALUE=2>Pittsburg Penguins
<OPTION VALUE=3>Toronto Maple Leafs
<OPTION VALUE=4>Ottawa Senators
</SELECT>
<BR>
<SELECT NAME="student" SIZE="1">
<OPTION>                    </OPTION>


But when I try to convert this into Struts code, it
just does not work! Its driving me crazy.

<html:select property="department"       
	onchange="populateDependent(this.form.student,
((this.selectedIndex == -1) ? null :
students[this.selectedIndex-1]));>
      <html:option value="-1">select
department</html:option>
      <html:options collection="departments"
property="id"labelProperty="description"/>
   </html:select>
</td> 
------------------
<html:select property="student" size="1">       
      <html:option value="-1"> select
student</html:option>
</html:select>

Could you guys tell me what is wrong with my struts
code? Thank you in advance!

regards,

rick


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: dependent list help!

Posted by Rick Col <ri...@yahoo.com>.
Thanks, steve:

Yes, I tried your code, it works. I got no error when
I run the program. I am not sure if the js function is
called, but it was fine when I ran the js code.

This is my JS file:
-------
students = new Array(
new Array(
new Array("Saku", 39482304),
new Array("Martin", 34802389),
new Array("Jeff", 39823498)
),
new Array(
new Array("Mario", 23840238),
new Array("Jaromir", 92390484),
new Array("Robert", 29048203),
new Array("Alexei", 94098230)
),
new Array(
new Array("Alexei", 20394802),
new Array("Daniel", 34982039),
new Array("Maria", 92348902),
new Array("Patrick", 98203894),
new Array("Radek", 98234902)
)
);

function populateDependent(selectCtrl, itemArray,
goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
	selectCtrl.options[i] = null; 
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
	if (prompt == null) {
	j = 0;
}
else {
	selectCtrl.options[0] = new Option(prompt);
	j = 1;
     }
if (itemArray != null) {
	// add new items
	for (i = 0; i < itemArray.length; i++) {
	selectCtrl.options[j] = new Option(itemArray[i][0]);
	if (itemArray[i][1] != null) {
		selectCtrl.options[j].value = itemArray[i][1]; 
	    }
	    j++;
     	}
	// select first item (prompt) for sub list
	selectCtrl.options[0].selected = true;
   }
}

This is my JSP file:
--------------------------
<%@ taglib uri="/WEB-INF/struts-bean.tld"
prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld"
prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld"
prefix="logic" %>

<html:html locale="true">
<head>
<title>stedent lists</title>
<html:base/>
<script language="JavaScript"
src="/dependent.js"></script>
</head>
<body bgcolor="white">

<html:form action="studentDisplayPage">
<table>
<tr>
<html:select property="department"       
	onchange="populateDependent(this.form.student,
((this.selectedIndex == -1) ? null :
students[this.selectedIndex-1]));>
      <html:option value="-1">select
department</html:option>
      <html:options collection="departments"
property="id"labelProperty="description"/>
   </html:select>
</td> 
</tr>
<tr>
<td>
<html:select property="student" size="1">       
      <html:option value="-1"> select
student</html:option>
</html:select>
</td>
</tr>
</table>
-----------------------------

I have tried this code million times. It just does not
populate dependent list. I am not sure what's wrong,
wish you guys can point out my mistakes.

regards,

Rick



--- Steven Santiago <sm...@neca.com> wrote:
> Rick,
> 
> Ah..misunderstood your original post. Coffee still
> working its way
> around. :) What errors are you getting? When I try
> with a simple
> onchange call it works fine. 
> 
> <html:select property="department"
> onchange="this.form.student.selectedIndex = 1;">
> 
> <html:select property="student" size="1">
> 	<html:option value="-1"> select
> student</html:option>
>       <html:option value="2">Tom</html:option>
> </html:select>
> 
> Sounds like it could be js related. Is your
> populateDependent function
> being called?
> 
> steve
> 
> 



__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: dependent list help!

Posted by Steven Santiago <sm...@neca.com>.
Rick,

Ah..misunderstood your original post. Coffee still working its way
around. :) What errors are you getting? When I try with a simple
onchange call it works fine. 

<html:select property="department"
onchange="this.form.student.selectedIndex = 1;">

<html:select property="student" size="1">
	<html:option value="-1"> select student</html:option>
      <html:option value="2">Tom</html:option>
</html:select>

Sounds like it could be js related. Is your populateDependent function
being called?

steve


-----Original Message-----
From: Rick Col [mailto:rick_cols@yahoo.com] 
Sent: Thursday, July 24, 2003 10:21 AM
To: Struts Users Mailing List; sms687@neca.com
Subject: RE: dependent list help!


Hi, steve:
The department list appears ok, but when I select one 
department, nothing happens to the student dependent
list. It works very well with html/javascript code
shown below. Can Struts do the same thing? thanks. 


--- Steven Santiago <sm...@neca.com> wrote:
> Rick,
> 
> Have you tried using optionsCollection? Maybe
> something like:
> 
> <html:select property="department">
> 	<html:option value="-1">select
> department</html:option>
>       <html:optionsCollection
> property="departments"
> label="description"  value="id"/>
> </html:select>
> 
> steve

> <SELECT NAME="department" 
> onChange="populateDependent(this.form.student,
> ((this.selectedIndex == -1) ? null : 
> students[this.selectedIndex-1]));">
> <OPTION VALUE="-1">Select department
> <OPTION VALUE=1>Math
> <OPTION VALUE=2>Chemistry
> <OPTION VALUE=3>Computer science
> <OPTION VALUE=4>English
> </SELECT>
> <BR>
> <SELECT NAME="student" SIZE="1">
> <OPTION>                    </OPTION>
> 
> 
> But when I try to convert this into Struts code, it
> just does not work! Its driving me crazy.
> 
> <html:select property="department"       
> 	onchange="populateDependent(this.form.student,
> ((this.selectedIndex == -1) ? null : 
> students[this.selectedIndex-1]));>
>       <html:option value="-1">select
> department</html:option>
>       <html:options collection="departments" 
> property="id"labelProperty="description"/>
>    </html:select>
> </td>
> ------------------
> <html:select property="student" size="1">       
>       <html:option value="-1"> select
> student</html:option>
> </html:select>
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: dependent list help!

Posted by Rick Col <ri...@yahoo.com>.
Hi, steve:
The department list appears ok, but when I select one 
department, nothing happens to the student dependent
list. It works very well with html/javascript code
shown below. Can Struts do the same thing? thanks. 


--- Steven Santiago <sm...@neca.com> wrote:
> Rick,
> 
> Have you tried using optionsCollection? Maybe
> something like:
> 
> <html:select property="department">
> 	<html:option value="-1">select
> department</html:option>
>       <html:optionsCollection 
> property="departments"
> label="description"  value="id"/>
> </html:select>
> 
> steve

> <SELECT NAME="department"
> onChange="populateDependent(this.form.student,
> ((this.selectedIndex == -1) ? null :
> students[this.selectedIndex-1]));">
> <OPTION VALUE="-1">Select department
> <OPTION VALUE=1>Math
> <OPTION VALUE=2>Chemistry
> <OPTION VALUE=3>Computer science
> <OPTION VALUE=4>English
> </SELECT>
> <BR>
> <SELECT NAME="student" SIZE="1">
> <OPTION>                    </OPTION>
> 
> 
> But when I try to convert this into Struts code, it
> just does not work! Its driving me crazy.
> 
> <html:select property="department"       
> 	onchange="populateDependent(this.form.student,
> ((this.selectedIndex == -1) ? null :
> students[this.selectedIndex-1]));>
>       <html:option value="-1">select
> department</html:option>
>       <html:options collection="departments"
> property="id"labelProperty="description"/>
>    </html:select>
> </td> 
> ------------------
> <html:select property="student" size="1">       
>       <html:option value="-1"> select
> student</html:option>
> </html:select>
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: dependent list help!

Posted by Steven Santiago <sm...@neca.com>.
Rick,

Have you tried using optionsCollection? Maybe something like:

<html:select property="department">
	<html:option value="-1">select department</html:option>
      <html:optionsCollection  property="departments"
label="description"  value="id"/>
</html:select>

steve


-----Original Message-----
From: Rick Col [mailto:rick_cols@yahoo.com] 
Sent: Thursday, July 24, 2003 9:27 AM
To: struts-user@jakarta.apache.org
Subject: dependent list help!


Hi,

I can use the follwoing javascript code to dynamically
fill a dependent list (students is aa array of
arrays).

<SELECT NAME="department" onChange="populateDependent(this.form.student,
((this.selectedIndex == -1) ? null : students[this.selectedIndex-1]));">
<OPTION VALUE="-1">Select Team
<OPTION VALUE=1>Montreal Canadiens
<OPTION VALUE=2>Pittsburg Penguins
<OPTION VALUE=3>Toronto Maple Leafs
<OPTION VALUE=4>Ottawa Senators
</SELECT>
<BR>
<SELECT NAME="student" SIZE="1">
<OPTION>                    </OPTION>


But when I try to convert this into Struts code, it
just does not work! Its driving me crazy.

<html:select property="department"       
	onchange="populateDependent(this.form.student,
((this.selectedIndex == -1) ? null : students[this.selectedIndex-1]));>
      <html:option value="-1">select
department</html:option>
      <html:options collection="departments"
property="id"labelProperty="description"/>
   </html:select>
</td> 
------------------
<html:select property="student" size="1">       
      <html:option value="-1"> select
student</html:option>
</html:select>

Could you guys tell me what is wrong with my struts
code? Thank you in advance!

regards,

rick


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org