Question

I'm trying to dynamically populate some optgroups for my select box in struts. The purpose is to group any duplicate names in the map that drives the select box control into their own option groups.

For example, I have 3 companies:

  • Home Depot
  • McDonald's
  • Wal Mart

In my database, i have multiple locations:

  • Home Depot
    -38393 Some Street
    -3839 Elm Street
  • McDonald's
    -38393 Street Ave
    -38333 Test Street
    -393339 This Street
  • Wal Mart
    -3939 Street Ave

and i simply want them grouped in my dropdown using option groups (only if there are multiple addresses, otherwise I want them displayed normally in the dropdown) - something probably sort of similar to this example i found. I want to do something sort of similar to below, however the optgroup has to be linked to the option somehow.:

<s:select id="regionstate" name="state" list="stateMap">  
             <s:iterator value="region" status="regionStatus">  
                 <optgroup label="<s:property value="name" />">  
                         <s:iterator value="states" status="stateStatus">  
                             <option value="<s:property value="id" />"><s:property value="name" /></option>  
                         </s:iterator>  
                 </optgroup>  
     </s:iterator>  

I haven't actually implemented the code above, but I believe the problem with the example as it relates to my issue is that it doesn't appear that the regions and states are linked.

I have: - a list of my duplicates (the names), that would populate the label attribute of the optgroup, and - a map that includes the appropriate value (dropdown value) for each individual item and the address for each item (displayed to the user)

I'm not sure if what i'm trying to achieve is even possible, but it seems like it should be pretty straight-forward. I'm hoping I just need a little direction and there's just a small piece of the puzzle i'm missing.

Thanks in advance for all your help!

It should look something like this, i think:

  • -select
    • optgroup 1
      • option
      • option
        -optgroup 2
      • option
      • option
      • option
        -optgroup 3
      • option
      • option

Sorry for all the confusion with the edits. the way i understand it, an optgroup inside the select box would be a list of the duplicated names (McDonald's, Home Depot, Wal Mart). Each option within each optgroup would be tied to that specific name. So for example, the optgroup for McDonald's could have multiple options associated with it.


Ok let's say i have a company class. The class has 3 properties: id, name and address. Here is how i expect the rendered select box to look:

<select name="companies">
    <option value="1a">Company 1</option>
    <option value="2a">Company 2</option>
    <optgroup label="Company 3">
        <option value="3a">38373 Street Ave</option>
        <option value="3b">38393 Town St</option>
    </optgroup>
    <optgroup label="Company 4">
        <option value="4a">990300 Street Ave</option>
        <option value="4b">99093 Town St</option>
        <option value="4c">99093 Town St</option>
    </optgroup>
</select>
  • I have a map that has the id and company name for each company that is unique (company 1 and company 2). that is populated by way of the list attribute for the s:select tag.
  • I expect that i need a list of the company names for each item that is duplicated (company 3 and company 4) for the purpose of populating the optgroup label attribute for each optgroup. My assumption is that each item in the list would need to correspond to one of the maps. This would require an iterator to dynamically create multiple optgroups
  • I also expect that i need to have a different map that has the id and company address for each item that is duplicated (company 3 and company 4). This would also require an iterator to dynamically create the multiple options within each optgroup.

Perhaps the thing I'm having trouble conveying is that there is actually 3 bits of data that I need to work with, so I'm not sure that just using a map will do. Like i said, it's very possible that i'm missing a piece of this puzzle or that my assumed design is flawed.

Was it helpful?

Solution

Just try the following code it may help you .

    <s:select id="regionstate" name="state" list="stateMap">  
                 <s:iterator value="region" status="regionStatus">  
                     <optgroup label="%{name}">  
                             <s:iterator value="states" status="stateStatus">  
                                 <option value="%{id}"> %{name}</option>  
                             </s:iterator>  
                     </optgroup>  
               </s:iterator>
    </s:select>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top