tags:

views:

585

answers:

4

Well new to the java stuff and having trouble with assigning a class attribute to this SELECT options. wanted to know if the listClass is correct? What I'm trying to accomplish is to add a class attribute to the options of a SELECT drop down

What I have:

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="ford">F150</option>
</select>

What I would like:

<select>
  <option class="car" value="volvo">Volvo</option>
  <option class="car" value="saab">Saab</option>
  <option class="car" value="mercedes">Mercedes</option>
  <option class="truck" value="ford">F150</option>
</select>

Here is the JSP code:

<s:select id="theSelectId" 
   name="theDto.id" 
   value="#attr.theDto.field_id" 
   list="theDtoList" 
   listKey="field_id" 
   listValue="field_value"/>

Is there a listClass? or another way of pulling the values that I need?

A: 

I just did a quick check of the Struts tag lib API and it looks like the styleClass attribute for the Struts select tag will create a class attribute.

http://struts.apache.org/1.x/struts-taglib/tlddoc/html/select.html

Mr. Will
Hmm thanks for the link and I added the styleClass="theDto.field_id" but this shows up as unidentified attribute name. I can use cssClass but this only adds the class to the SELECT tag and not the options as well as it doesn't pull the object values for the options as well.
Phill Pafford
A: 

For this I suspect that you will have to roll your own tag. You can create a new myselect tag that accepts a optionClass parameter that sets that class to each option element.

Vincent Ramdhanie
this sounds like the solution I need but have no idea how to write my own tag, any thoughts as to where I could start?
Phill Pafford
Are you using struts1.x or struts2?
Vincent Ramdhanie
A: 

add the classes in manually using a javascript library? jquery perhaps?

Titi Wangsa bin Damhore
A: 

Sounds like Struts s:select has the same problem as JSF h:selectOneMenu: the unability to specifiy a class for each individual option. I don't do Sruts, so I can't give a detailed and Struts-specific answer, but to the point, you need to change/override the renderer of the s:select component so that it takes an extra attribute which you finally can use to specify the option classes. Something like as done in this JSF example.

If you fail to do so in the server side due to limitations in struts or limitations in your capabilities, then consider using a Javascript library like the aforementioned jQuery. An example would be then:

var optionClasses = ['volvo', 'saab', 'mercedes'];
$('#dropdownId option').each(function(index, optionElement) {
    $(optionElement).class(optionClasses[index]);
});
BalusC