10

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
vendetta91
  • 103
  • 1
  • 1
  • 5

5 Answers5

6

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>
Aritz
  • 30,971
  • 16
  • 136
  • 217
McDowell
  • 107,573
  • 31
  • 204
  • 267
0

Assume your <h:selectOneMenu is as below.

<h:form id="myForm">
  <h:selectOneMenu id="myCombo">
    <f:selectItems value="#{foo.fooList}"/>
  </h:selectOneMenu>
</h:form>

Now at window.onload you can iterate through options and add the title as below

<script>
    window.onload = function() {
         var options = document.getElementById("myForm:myCombo").options;
         for(var i = 0; i &lt; options.length; i++) {
             options[i].title = "description" + i;
         }
    }
</script>
Aritz
  • 30,971
  • 16
  • 136
  • 217
prageeth
  • 7,159
  • 7
  • 44
  • 72
0

itemDescription attribute will not appear in seam 2.2.

Best solution would be using javascript to show tooltip for each select item.

 <script type="text\javascript">
   function getTooltip(id){
     var options = document.getElementById(id).options;
         for(var i = 0; i &lt; options.length; i++) {
            options[i].title = "description" + i;
         }
    }
 </script>

if your id is generated dynamically or other way, for eg.

 <rich:dataTable value="#{mybean.list}">
   <h:selectOneMenu value="#{mybean.selectedValue}" onmouseover=getTooltip(this.id)>
     <f:selectitems value="#{mybean.selectlist}">
   </h:selectOneMenu>
 </rich:datatable>

This solution will work for all dynamically generated id's as well as simple

0

To generate a title attribute on your generated options, you can simply use passthrough attributes like this:

<f:selectItems value="#{values}" p:title="Your title here"/>
tfosra
  • 581
  • 5
  • 12
  • 1
    Unfortunately you can't use the variable defined via `` inside the passthrough-attribute :(. But BalusC show in [this answer](http://stackoverflow.com/questions/25511351/how-to-add-tooltip-to-fselectitems#25512124) how to work around this limitation via `c:forEach`. – Martin Höller Feb 28 '17 at 13:45
0

I think for f:selectItems tag there is no such (title) attribute available. You have this attribute in plain option tag in HTML but not in jsf. I think you should use plain select tag instead of selectOneMenu to get title value.

prageeth
  • 7,159
  • 7
  • 44
  • 72
  • 1
    "You have this attribute in plain option tag in HTML" - its widespread adoption is quite a recent thing though, it is only supported from IE9 and up as far as I can tell. If there isn't one already, a feature request in the JSF jira on javaserverfaces.java.net is probably not a bad thing. – Gimby Jan 07 '13 at 09:26