<f:selectItems> JSF标记自定义工具提示属性

Ale*_*kor 9 tags jsf attributes title

是否可以在JSF中为标记添加"title"属性,例如:

<f:selectItems value="#{foo.fooList}" title="{foo.fooDescription}"/>
Run Code Online (Sandbox Code Playgroud)

生成的HTML:

<select>
    <option value="foo1" title="description1">foo ex1</option>
    <option value="foo2" title="description2">foo ex2</option>
</select>
Run Code Online (Sandbox Code Playgroud)

McD*_*ell 5

我没有一个优雅的解决方案,但它可以做到.我假设JSF 2+和Facelets VDL.

对于托管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;
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用JavaScript title在DOM节点上设置属性:

<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>
Run Code Online (Sandbox Code Playgroud)