0

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:selectItemscomponents 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

Community
  • 1
  • 1
user3280015
  • 279
  • 2
  • 10
  • 1
    Why don't you make use of `` in first place? – BalusC Aug 27 '14 at 18:46
  • @BalusC because I'm stupid... -.- You are right, I can also use it for the input to my CustomMenuRenderer. Now I'm using `writer.writeAttribute("title", curItem.getDescription(), "description");` in the CustomMenuRenderer and `` in the facelet. I dropped the inner f:attribute. But now I am not seeing `"innerTEST"` either in the output, any idea what I am still doing wrong? – user3280015 Aug 28 '14 at 08:00

0 Answers0