Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Bear, I'll take a look at the code, thanks!
 
reply
    Bookmark Topic Watch Topic
  • New Topic