Here is one way to do what you are attempting in javascript

Lets assume you have a group of divisions and a larger group of deparments
which each belong to one of the divisions.

The divisions have ID values and the departments have a parent division in
the database

(I am utilizing scriptlets instead of a bean for clarity)

See the bottom for more info and explanations :)

-----------------------
<SCRIPT LANGUAGE="javascript">

function dynamicDept() {
   var deptValue = new Array;
   var deptList = new Array;
<%
  ResultSet result = dbConnection.query("select ID, Name from top order by
ID");
  while(result.next()) {
%>
   // An array of all the divisions
   deptValue[<%= result.getString(1) %>] = "<%= result.getString(2) %>"
<%
  }
  for (int i=0; i<5; i++) {  // there are 5 divisions (assume), query for
each division
    result = d.query("select ID from departments where division = " +i+ "
order by Name");
    if (result.next()) {
%>
   // deptList is the name of departments that belong to the divisions (ID 1
thru ID 15)
   deptList[<%= i %>] = new Array(<%= result.getString(1) %>

<% while (result.next()) { %>

      ,<%= result.getString(1) %>

<% } %>

   );

<% }} %>

   divID = document.request.division.selectedIndex;   // which division was
selected in the drop down?
   numChildren = document.request.department.length;  // number of children
in this division

   // remove all elements in the department list to prepare for the new
division population
   for (i = numChildren; i > 0; i--) {
     document.request.department.options[i] = null;
   }

   numChildren = deptList[divID].length; // get the number of departments in
the newly selected division
   document.request.department.length = numChildren;  // set length of
department select item to the number of departments

   // add elements to the list
   for (i = 0; i < numChildren; i++) {
     deptID = deptList[divID][i];
     deptName = deptValue[deptID];
     document.request.department.options[i+1] = new Option(deptName,
deptID);
   }
}

---------------------------------------

The results after the page has been generated should look similar to this .
. .

function dynamicDept() {
   var deptValue = new Array;
   var deptList = new Array;

   // all the departments in the first array
   deptValue[1] = "Department 1"
   deptValue[2] = "Department 2"
   deptValue[3] = "Department 3"
   deptValue[4] = "Department 4"
   deptValue[6] = "Department 5"
   etc etc . . .

   // the list of departments for division 1 (Department 1 and Department 6)
   deptList[1] = new Array(1
          ,6
          );

   // the list of departments for division 2 (Department 2 and Department 3)
   deptList[2] = new Array(2
          ,3
          );

   // the list of departments for division 3 (Department 4 and Department 5)
   deptList[3] = new Array(4
         ,5
          );

  // and so on . . .

  // here is where the actual work is done . . .
  divID = document.request.division.selectedIndex;
  numChildren = document.request.department.length;

  // remove all elements in the list
  for (i = numChildren; i > 0; i--) {
    document.request.department.options[i] = null;
  }

  numChildren = deptList[divID].length;
  document.request.department.length = numChildren;

  // add elements to the list
  for (i = 0; i < numChildren; i++) {
    deptID = deptList[divID][i];
    deptName = deptValue[deptID];
    document.request.department.options[i+1] = new Option(deptName, deptID);
  }
}


Then all you need to do is have your select box call the function.

There is some serious overhead with this type of functionality, so you may
want to evaluate your other options prior to implementing this.  The one
advantage is that you do not have to "reload" the page unnecessarily as all
the logic is done in the browser.  Some of the major drawbacks are, people
who turn off javascript (which I never have to worry about where i work),
and the overhead of so many queries, which can be easily solved in a variety
of ways.  The biggest issue I have noticed with this method is very few
people understand it well enough to modify it when necessary, which means
more work for me when something needs changing.  Another option you may wish
to consider is a small applett (depending on your web server, db, and
security concerns) that can perform the necessary queries dynamically, so
you dont have the overhead of calling so many subqueries as in the above
example. . .

I have implemented this a few times successfully using different styles but
this one seems to work the best for most of the browsers

Hope this helps ya out a bit :)

Take care

J




> -----Original Message-----
> From: Huang, Dennis [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, December 16, 1999 7:43 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Form Resubmits with JSP
>
>
> 1. First you have to make form submit to itself( I mean
> action="thisPage.jsp")
> 2. Use javascript to create querystring(which item user
> selects) and control
> submit().
>
> Hope you can understand what I mean.
>
> Dennis Huang
>
>
> -----Original Message-----
> From: Thompson, Brad [mailto:[EMAIL PROTECTED]]
> Sent: Friday, 17 December 1999 10:25
> To: [EMAIL PROTECTED]
> Subject: Form Resubmits with JSP
>
>
> I am creating a form that contains multiple fields that are drop-down
> combination boxes (selection boxes).  I need to figure out a way to
> populate the second drop down box (Department field) based on
> the selection
> the user chose in the previous selection field (Agency).
>
> From what I understand, I should be able to do multiple
> submits of the form
> in JSP.  The first would pass the agency to my servlet which
> would in turn
> call my backend CORBA services to return the possible
> Department values to
> the form.  Then after the user selects the Department value
> the form would
> be resubmitted.
>
> code snippet....
>     <TR>
>       <TD><STRONG>Agency:</STRONG></TD>
>       <TD><SELECT name=agency style="HEIGHT: 22px; WIDTH:
> 88px"></SELECT></TD>
>       <TD><STRONG>Department:</STRONG></TD>
>       <TD><SELECT name=department style="HEIGHT: 22px; WIDTH:
> 84px"></SELECT></TD>
>
> Is this possible?  If so, does anyone have example code on how to do
> something like this?  I'm new to the Java world and haven't
> been able to
> find any examples of multiple submissions of a form.  It seems pretty
> complicated to me.
>
> Any help would be much appreciated.
>
> Thanks,
> Brad
>
> ==============================================================
> =============
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>
> ==============================================================
> =============
> To unsubscribe: mailto [EMAIL PROTECTED] with body:
> "signoff JSP-INTEREST".
> FAQs on JSP can be found at:
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to