Hi David:


I want to be able to select an option in a dropdown
since I am creating my dropdowns via the singleton model. 
Since, my select objects are populated from a database, the
Singleton will eliminate unnecessary calls to the database.
Therefore, I need to first create my select objects, and
then select options in my dropdowns based on user interactions with
my JSP's.


Thanks for your input:

Alex Restrepo




-----Original Message-----
From: David Ethell [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 05, 2001 2:49 PM
To: [EMAIL PROTECTED]
Subject: RE: How do I Copy/Clone an "extended" Select Object????


Robert wrote:

> > QUESTION 1:
> > I have created a select object with
> > 4 options. How can I select the 3rd
> > option by value?
>
>  public org.apache.ecs.html.Select selectOption(
>                     org.apache.ecs.html.Select select,
>                     String value)
>   {
>   if (value!=null && select!=null)
>   {
>    for (Enumeration en=select.elements();en.hasMoreElements();)
>    {
>     Object next=en.nextElement();
>     if (next instanceof org.apache.ecs.html.Option)
>     {
>      org.apache.ecs.html.Option opt =(org.apache.ecs.html.Option) next;
>      if (opt.getAttribute("value").equals(value))
>      {
>        opt.setSelected(true);
>      } else {
>       opt.setSelected(false);
>      }
>     }
>    }
>   }
>   return select;
>  }

why do it in a complex loop like this? Why not set the selection as you
build each option:

Select mySelect = new Select("Test");
Option option1               =  new Option("Option 1");
Option option2               =  new Option("Option 2");
Option option3               =  new Option("Option 3");
Option option4               =  new Option("Option 4");
option1.setSelected(value.equals("Option 1")?true:false);
option2.setSelected(value.equals("Option 2")?true:false);
option3.setSelected(value.equals("Option 3")?true:false);
option4.setSelected(value.equals("Option 4")?true:false);
...

Granted, that gets messy if you have a lot of options in your Select, but if
that's the case why not do:

Select mySelect = new Select("Test");
for (int option_num = 1; option_num <= 4; option_num++) {
  mySelect.addElement(new Option("Option " + option_num)
    .addElement("HELLO " + option_num)
    .setSelected(value.equals("Option " + option_num)?true:false)
  );
}

Of course, your for loop will usually be some loop through a recordset from
a database in which case you'll use some other mechanism for naming the
options, but the principle is the same.

David


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to