• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marcin Kurek
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marcin Kurek
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marcin Kurek
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. Bear, I'll take a look at the code, thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic