I want to write a custom renderer for the h:selectOneMenu
component and eventually make use of the description
property of the UISelectItem
class to add a title
a.k.a. tooltip to f:selectItems
following BalusC's profound guides in https://stackoverflow.com/a/25512124/3280015 and http://balusc.blogspot.de/2008/08/styling-options-in-hselectonemenu.html.
Now I did extend the com.sun.faces.renderkit.html_basic.MenuRenderer
in my own CustomMenuRenderer
, registered it with the faces-config.xml
and overrode the renderOption
method, adding the following code before option tag is terminated by the Responsewriter
:
String titleAttributeValue = (String) component.getAttributes().get("title");
if (titleAttributeValue != null) {
String indexKey = component.getClientId(context)
+ "_currentOptionIndex";
Integer index = (Integer) component.getAttributes().get(indexKey);
if (index == null) {
index = 0;
}
component.getAttributes().put(indexKey, ++index);
}
I'm not quite sure I'm doing the indexKey
thing right or whether I need it for the title attribute or should use a writer.writeAttribute("title", titleAttributeValue, null);
instead because I don't have a list like in the optionClasses tutorial, but the code works so far!
In the actual view definition use case I did:
<h:selectOneMenu value="#{cc.data}">
<f:attribute name="title" value="outerTEST" />
<c:forEach items="#{cc.list}" var="val">
<f:selectItem value="#{val}" itemValue="#{val}" itemLabel="#{val.label}">
<f:attribute name="title" value="innerTEST #{val.description}" />
</f:selectItem>
</c:forEach>
</h:selectOneMenu>
(I just put the #{val.description}
there in the title value to clarify my intention, it is currently still empty and I will have to think about how to populate it per element later, but for the sake of the question we can assume it is already filled.)
But now I'm getting the "outerTEST"
properly showing up in the title attribute of the option in the resulting XHTML in the Browser, yet I'm not seeing any of the "innerTEST"
which would and should be individual per selectItem
and which is what this is eventually all about.
I understand the f:selectItem
and f:selectItems
components do not have their own renderers but rendering of options is generally handled by the MenuRenderer
via its renderOption
method.
But how then would I add individual titles to the individual selectItems??
Thanks