jacosta
jacosta

Reputation: 457

Struts 2 - Dynamically populate optgroup with iteration

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:

In my database, i have multiple locations:

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:

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>

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.

Upvotes: 2

Views: 4450

Answers (1)

sumit sharma
sumit sharma

Reputation: 1057

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>

Upvotes: 3

Related Questions