my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes How to add a select dynamically and populate it with values from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to add a select dynamically and populate it with values from database" Watch "How to add a select dynamically and populate it with values from database" New topic
Author

How to add a select dynamically and populate it with values from database

Marcin Kurek
Greenhorn

Joined: May 11, 2008
Posts: 8
Hello,

I've been googling for a while but still can't find solution to my problem.
I would like to have a jsp page with a form consisting of several inputs and selects. When page loads for the first time, there is only one select (with values taken from database), but after clicking on link "add", the subsequent selects are added (using JavaScript and DOM). It seems to be pretty easy task, but I'm not sure how to populate the newly created select with data.

Thanks!
Dustin Johnson
Greenhorn

Joined: Nov 14, 2006
Posts: 22
The way I've typically done what you're trying to do is with the following:

1) Create a TAG class that will create the select boxes (usually one class per select box).
2) Place the select boxes in an html div that should be hidden initially.
3) On the onclick attribute of the link "Add" it would call a javascript function, which would set the divs visible that were hidden, and perform whatever additional logic is necessary.
4) Since the selects were generated from the page load, they alread have all of the data you need from the database, you just waited for the cue to display them.


SCJA 86%<br />SCJP 5.0 98%<br />SCWCD 1.4 79%<br />SCBCD 5.0 83%<br />--------------------------------<br />"I drank what???" - Socrates
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Dustin's suggestion would work as long as you have a static set of SELECT elements on the page. If it needs to be more dynamic look into things like document.createElement() and the Option object in JavaScript.


GenRocket - Experts at Building Test Data
Marcin Kurek
Greenhorn

Joined: May 11, 2008
Posts: 8
OK, so let's suppose I have three selects generated on page load, thus populated with data. Two of them are hidden by default. What happens if I don't click "Add" link and submit the form? I believe the hidden selects are submitted too. But at the servlet logic I need to distinguish them somehow.
For example:
Select 1 (visible)
- attribute 1
- attribute 2 (selected by user)
- attribute 3
Select 2 (hidden)
- attribute 1 (selected by default)
- attribute 2
- attribute 3
At the servlet code, how do I know that Select 1 was selected by user and Select 2 wasn't even visible, so it does not need processing?
Marcin Kurek
Greenhorn

Joined: May 11, 2008
Posts: 8
Gregg,

I could create new option this way:

Document.forms['myform'].testselect.options[0] = new Option('new text','new value');

But the point is that I need to get data from the database.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

This is a classical Ajax example. Upon selection of the first dropdown, and Ajax request is performed to obtain the values with which to populate the second (and third, and so on). There are examples all over the net. Search for "cascading dropdowns" or "dependent dropdowns".

If you are using jQuery to do your Ajax for you (highly recommended), I have code examples that do this in the example code for my book on jQuery. You can download the code from the book's web page. The code for chapter 8 has the Ajax examples.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marcin Kurek
Greenhorn

Joined: May 11, 2008
Posts: 8
Thanks guys. Bear, I'll take a look at the code, thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add a select dynamically and populate it with values from database