Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<f:selectItems> JSF tag custom tooltip attribute

Is it possible to add a "title" attribute to the tag in JSF, for example:

<f:selectItems value="#{foo.fooList}" title="{foo.fooDescription}"/>

Generated HTML:

<select>
    <option value="foo1" title="description1">foo ex1</option>
    <option value="foo2" title="description2">foo ex2</option>
</select>
like image 424
vendetta91 Avatar asked Mar 21 '12 08:03

vendetta91


1 Answers

I don't have an elegant solution, but it can be done. I'm assuming JSF 2+ & Facelets VDL.

For a managed bean Foo:

@ManagedBean @RequestScoped
public class Foo {
  private List<SelectItem> fooList = Arrays.asList(
            new SelectItem("value1", "label1", "description1"),
            new SelectItem("value2", "label2", "description2"));

  public List<SelectItem> getFooList() {
    return fooList;
  }
}

You can use JavaScript to set the title attribute on the DOM node:

<h:selectOneMenu binding="#{requestScope.fooSelectOne}">
  <f:selectItems value="#{foo.fooList}" />
</h:selectOneMenu>
<script>
(function() {
  var selectName = '#{requestScope.fooSelectOne.clientId}';
  var kids = document.getElementsByName(selectName)[0]
                     .getElementsByTagName("option");
  var index = 0;
  <ui:repeat value="#{foo.fooList}" var="_opt">
  kids[index++].title = '#{_opt.description}'; //TODO: escape this
  </ui:repeat>
}());
</script>
like image 157
McDowell Avatar answered Sep 27 '22 20:09

McDowell