使用工具提示动态创建selectManyCheckbox

时间:2012-12-17 21:07:47

标签: jsf richfaces tooltip selectmanycheckbox

我在支持bean中有一个HashMap,我想动态渲染多个选择框。这是代码:

    <h:selectManyCheckbox
      id="id"
      value="#{backingBean.value}"
      layout="pageDirection"
      styleClass="label"
      required="true"
      requiredMessage="You must select at least...">


      <a4j:repeat var="aGroup" items="#{backingBean.map}">

        <f:selectItem id="role#{aGroup.value.property1}" itemLabel="#{aGroup.value.property1}" itemValue="#{aGroup.value.property2}" />

        <rich:tooltip for="role" value="#{aGroup.value.property5}" />

     </a4j:repeat>

    </h:selectManyCheckbox> 

它不是渲染。 使用f:selectItems标签,它正在渲染,但我需要手动创建f:selecteItem,因为我必须附加一个rich:tooltip和f:selectItem。

有什么想法吗?

拉​​维

2 个答案:

答案 0 :(得分:1)

您无法在<f:selectItem>上添加其他标记/组件。

标准JSF或RichFaces库中也没有任何内容允许在复选框组的标签上自由标记。将layout="spread"<t:checkbox>一起使用时,只有Tomahawk <t:selectManyCheckbox>支持此功能。 PrimeFaces的<p:selectManyCheckbox>也有on schedule这个功能。

以下是如何使用Tomahawk实现它的启动示例:

<!-- Below displays nothing due to layout="spread". -->
<t:selectManyCheckbox id="foo" value="#{bean.selectedItems}" layout="spread">
    <f:selectItems value="#{bean.availableItems}" />
</t:selectManyCheckbox>

<!-- Below displays the concrete checkboxes with labels. -->
<c:forEach items="#{bean.availableItems}" var="item" varStatus="loop">
    <t:checkbox for="foo" index="#{loop.index}" />
    <h:outputLabel id="label#{loop.index}" value="#{item.label}" />
    <rich:tooltip for="label#{loop.index}" value="#{item.tooltip}" />
</c:forEach>

答案 1 :(得分:0)

使用<c:forEach>代替<a4j:repeat>

首先在页面顶部添加此命名空间。

xmlns:c="http://java.sun.com/jstl/core"

现在用此替换<a4j:repeat.....

<c:forEach var="aGroup" items="#{backingBean.map}">

编辑:

<小时/> 我不认为<rich:toolTip>可以应用于<f:selectItem>。作为一个奇怪的黑客你可以做这样的事情。

这是managed-bean,它会返回listmap。我在这里使用地图,因为看起来你正在使用地图。

public class MyBean {
  private Map<Integer, String> map;

  public Map<Integer, String> getMap() {
    map = new HashMap<Integer, String>();
    map.put(1, "Tool tip 1");
    map.put(2, "Tool tip 2");
    map.put(3, "Tool tip 3");
    return map;
  }
}

现在您的xhtml代码就像这样。这里我动态地渲染selectItem。但是,这不是必须的。

<h:form id="myForm">
    <h:selectManyCheckbox id="myChkBox" layout="pageDirection" styleClass="label" required="true" requiredMessage="You must select at least...">
        <c:forEach var="aGroup" items="#{myBean.map}">
            <f:selectItem itemValue="someValue1" itemLabel="someValue1" id="someId#{aGroup.key}" /> 
            <span class="rich-tool-tip" id="span#{aGroup.key}" style="z-index: 99; visibility: visible; display: none;">
                <span>#{aGroup.value}</span>
            </span>
            <script>
                var i = !i ? 0 : i;
                new ToolTip("span#{aGroup.key}","myForm:myChkBox:" + i,{'showEvent':'mouseover'} );
                i++;
            </script>
        </c:forEach>
    </h:selectManyCheckbox>
    <rich:toolTip rendered="false"/>
</h:form>

就是这样......
请注意<rich:toolTip>设置为rendered="false"。这是必需的。否则,某些重要的JS部分不会导入,而toolTip将无效。

相关问题