问题
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>
回答1:
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>
回答2:
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.
回答3:
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 option
s and add the title
as below
<script>
window.onload = function() {
var options = document.getElementById("myForm:myCombo").options;
for(var i = 0; i < options.length; i++) {
options[i].title = "description" + i;
}
}
</script>
回答4:
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 < 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
回答5:
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"/>
来源:https://stackoverflow.com/questions/9800807/fselectitems-jsf-tag-custom-tooltip-attribute