[英]<f:selectItems> JSF tag custom tooltip attribute
是否可以在JSF中为标记添加“title”属性,例如:
<f:selectItems value="#{foo.fooList}" title="{foo.fooDescription}"/>
生成的HTML:
<select>
<option value="foo1" title="description1">foo ex1</option>
<option value="foo2" title="description2">foo ex2</option>
</select>
我没有一个优雅的解决方案,但它可以做到。 我假设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;
}
}
您可以使用JavaScript在DOM节点上设置title
属性:
<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>
假设您的<h:selectOneMenu
如下所示。
<h:form id="myForm">
<h:selectOneMenu id="myCombo">
<f:selectItems value="#{foo.fooList}"/>
</h:selectOneMenu>
</h:form>
现在在window.onload
您可以遍历option
并添加title
,如下所示
<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>
itemDescription属性不会出现在seam 2.2中。
最佳解决方案是使用javascript显示每个选择项的工具提示。
<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>
如果你的id是动态或其他方式生成的,例如。
<rich:dataTable value="#{mybean.list}">
<h:selectOneMenu value="#{mybean.selectedValue}" onmouseover=getTooltip(this.id)>
<f:selectitems value="#{mybean.selectlist}">
</h:selectOneMenu>
</rich:datatable>
该解决方案适用于所有动态生成的id以及简单的id
要在生成的options
上生成title
属性,您只需使用如下的passthrough属性:
<f:selectItems value="#{values}" p:title="Your title here"/>
我认为对于f:selectItems
标签没有这样的( title
)属性可用。 您在HTML
中的普通option
标记中具有此属性,但在jsf
没有。 我认为您应该使用普通的select
标记而不是selectOneMenu
来获取title
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.