Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply CSS to <f:selectItem> nested in <h:selectOneMenu>

Tags:

css

jsf

I want to apply CSS specific to <f:selectItem> in <h:selectOneMenu> to be displayed in a different style.

e.g. I want every option of coffee in the list below to be displayed in a different color.

<h:selectOneMenu value="#{user.favCoffee1}"> 
   <f:selectItem itemValue="Cream Latte"   itemLabel="Coffee3 - Cream Latte" /> 
   <f:selectItem itemValue="Extreme Mocha" itemLabel="Coffee3 - Extreme Mocha" /> 
   <f:selectItem itemValue="Buena Vista"   itemLabel="Coffee3 - Buena Vista" /> 
</h:selectOneMenu>`

Can anyone help me out here?

like image 398
Sana Avatar asked Jul 19 '11 12:07

Sana


1 Answers

The <f:selectItem> renders a HTML <option> element. It has very limited CSS styling support. The color property is not among them. Even more, it works in MSIE only, not in other webbrowsers. There is however no way to give each <option> element its own style attribute by JSF, so closest what you can get is applying a CSS rule on all options and accepting that it works in MSIE only.

<h:selectOneMenu styleClass="mymenu">

with

.mymenu option {
    color: red;
}

Your best bet is to replace the dropdown by a <ul><li> with a good shot of CSS/JavaScript which mimics it to look like a dropdown. Some JSF component libraries has such a component, such as PrimeFaces' <p:selectOneMenu>. Check the Custom content example in its 3.0 showcase.

like image 136
BalusC Avatar answered Oct 13 '22 13:10

BalusC