Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating a dependant select list without reloading the page

 
Vanessa Danin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP which includes 2 select lists. The option selected from the first select list determines which set of options is displayed in the second select list.

The first select list always consists of the same, few options whereas the second select list's options may be numerous and importantly, are dynamic not only in terms of option list dependancy on the first select list but also because the values that make up each possible set of option values for the second select list are based on editable database values and therefore may change.

Currently, when I select an option from the first select list, I submit and reload the page to send through the selected option as a request parameter and use it to set a pageContext attribute in the reloaded page. I then test this pageContext attribute against several 'if' conditions to identify which is the correct set of options to display for the second select list. Then, I iterate through the set of values of the session-scoped attribute which applies to the matched 'if' condition. The session-scoped attribute holds a set of values retrieved from the database which are finally written out to the page as the second select list's option values.

My question is this: is there a way to display the relevant options for the second select list without reloading the page?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only way is with JavaScript by loading all of the options for the second Dropdown into arrays. If you are talking about tons and tons and tons of options then it will cause your page loading time to increase.

More info: see http://www.www.JavaScriptkit.com for double combo scripts.

Eric
 
Vanessa Danin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your reply Eric. Yes, I had decided against using Javascript arrays for that reason. I was hoping that there was another alternative but as there does not appear to be one I will continue using my existing code. Thanks once again for taking the time to assist me in making my decision.
 
Daniel Hedrick
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to send anyone away from Javaranch, but I recently came across this very useful tidbit that I think might be useful if you have a considerable amount of data to shuffle about:

http://developer.apple.com/internet/webcontent/iframe.html

Good luck!

-daniel
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the HTML/Javascript forum.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More involved but possible is to load JUST the select as a separate JSP using the capabilities of javascript to do requests to a webserver and inject the resulting data (html code in this case) into an html element (td or div most likely).
That's what we do sometimes and it works (except for a spurious javascript error which happens sometimes and seems unrelated to the mechanism itself).

The actual code is proprietary and I can't give it to you but the principle isn't that difficult.
 
Vanessa Danin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Daniel and Jeroen. Daniel, I found the article you attached to be very helpful and am currently experimenting with the remote scripting methods discussed as a possible solution to my problem.

The principle of what Jeroen suggests is encompassed by the remote scripting concept.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic