File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Populating DropDown Boxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Populating DropDown Boxes " Watch "Populating DropDown Boxes " New topic
Author

Populating DropDown Boxes

Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

I have a JSP that’s used for Data Entry. The JSP gets its initial values from a Database. I have several drop Down Boxes. The code below is how I handle one of the dropdown boxes that has only 3 choices. So I’m using “choose and when” logic. But I have others that have 30 choices and 40 choices. For those 2 drop down boxes I currently display the value and all the choices, which means the value from the DB is repeated. Is there another way besides how I coded my example, to not display a choice twice ? Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

I'd create a Map of the possible entries and use that to populate the drop down with a single forEach loop.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

Bear, I think I understand please confirm I got it right. So I get the selected="selected" , value from the database , then I loop thru the map lets say 40 times and I use an If statement inside the loop to see if it matches my database value and if it does I skip that one value. and just display the other 39.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

Are the entires themselves also coming from the database, or just the info on which one is to be selected?

If the entries come from the DB, I'd create a Map of all of them using the Map entry key as the option value, and the Map entry value as the display text. Loop through this Map with forEach in order to create the option elements. In that loop, test to see which option value matches the one to be selected, and add the selected attribute to that option only.
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

Should have gave more info. This JSP is an Update Screen. The intital Screen is loaded from a current DB record. Thats where I get the "Selected" Value from. This field is updatable. The optional values are listed in the dropdown box. I have (for now ) hard coded the 40 optional values. I want to elminate the one duplicate value I have. The selected value from the DB is also in the hard coded optional list.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

What do you mean by "update screen"? Regardless, there aren't any different modes, such as "update". JSP is JSP. If you want to make dynamic output, you may need to back away from the hard-coding.

Where are the original options coming from? Are they hard-coded on multiple pages?
Joe Brigs
Ranch Hand

Joined: Jun 06, 2011
Posts: 60

Sorry Bear, I'm stuck in the past. I meant "Update" to mean JSP brings back an existing record from database and the JSP will allow Updates to this record. Let’s say Acct 123, I have a status field that is contained in ${currentSVC_STAT_CD}, this field is set to "READY" , But the USER can Update this field ( or NOT ). I display this field as the "Selected" value in a dropdown box along with two other choices "A" & "B". So the way I currently coded it, The values would be displayed in the following order. Selected value = "ready", then if they drop down the box. they see (1) A, (2) B, (3) ready. In this case I don't want to repeat the "ready" , because that’s the value coming from the database. No big deal, but some users don't like that. So if I take your previous suggestions, (1) don’t hard code values, put in map or array, and then loop thru map. If value from DB matches occurrence in array, don't display it? I guess this is a simple problem, just looking for confirmation that it’s a good approach. Hope I explained what I'm trying to do, better than my previous posts. Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61180
    
  66

OK. Sounds like a viable approach now. You definitely don't want to repeat options, and by not hardcoding the value, you keep the list more flexible.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Populating DropDown Boxes