简体   繁体   English

p:selectManyCheckbox 或其他 p:selectMany*/One* 的 Primefaces 工具提示

[英]Primefaces tooltip for p:selectManyCheckbox or other p:selectMany*/One*

I would like to add a tooltip for each element in a p:selectManyCheckBox .我想为p:selectManyCheckBox每个元素添加一个工具提示。 However I can't come up with a solution.但是我想不出解决办法。

I've got a class Role that has 3 properties, "id" (Long), "name" (String) and "description" (String).我有一个Role类,它有 3 个属性,“id”(Long)、“name”(String)和“description”(String)。 The name is displayed and I would like to have the description as a tooltip.显示名称,我希望将描述作为工具提示。

This is a working piece of code:这是一段工作代码:

<p:selectManyCheckbox layout="pageDirection" value="#{roleBean.selectedRoles}" converter="roleConverter">
    <f:selectItems value="#{roleBean.roles}" var="role" itemLabel="#{role.name}" itemValue="#{role}"/>
</p:selectManyCheckbox>

The roleConverter is a FacesConverter that converts the Role to an id and visa versa. roleConverter是一个FacesConverter ,它将Role转换为 id,反之亦然。

I came up with this:我想出了这个:

<p:selectManyCheckbox layout="pageDirection" value="#{roleBean.selectedRoles}" converter="roleConverter">
    <c:forEach var="role" items="#{roleBean.roles}">
        <f:selectItem id="role#{role.id}" itemLabel="#{role.name}" itemValue="#{role}" />
        <p:tooltip for="role#{role.id}" value="#{role.description}"/>
    </c:forEach>
</p:selectManyCheckbox>

But unfortunately it doesn't work.但不幸的是它不起作用。

You can achieve this by using the SelectItem#getDescription() property as below:您可以通过使用SelectItem#getDescription()属性来实现这一点,如下所示:

<p:selectManyCheckbox layout="pageDirection"
    value="#{roleBean.selectedRoles}" converter="roleConverter">
    <f:selectItems value="#{roleBean.roles}" var="role" 
        itemValue="#{role}" itemLabel="#{role.name}" 
        itemDescription="#{role.description}" />
</p:selectManyCheckbox>

This is supported since PrimeFaces 6.2 ( actually because of this very answer you're reading now ).自 PrimeFaces 6.2 起就支持此功能(实际上是因为您现在正在阅读的这个答案)。

In case you're not on PrimeFaces 6.2 yet and cannot upgrade for some reason, then you need to manually override the PrimeFaces SelectManyCheckboxRenderer#encodeOptionLabel() as below in order to recognize and render it:如果您还没有使用 PrimeFaces 6.2 并且由于某种原因无法升级,那么您需要手动覆盖 PrimeFaces SelectManyCheckboxRenderer#encodeOptionLabel()如下以便识别和渲染它:

public class YourSelectManyCheckboxRenderer extends SelectManyCheckboxRenderer {

    @Override
    protected void encodeOptionLabel(FacesContext context, SelectManyCheckbox checkbox, String containerClientId, SelectItem option, boolean disabled) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("label", null);
        writer.writeAttribute("for", containerClientId, null);

        if (option.getDescription() != null) {
            writer.writeAttribute("title", option.getDescription(), null);
        }

        if (disabled) {
            writer.writeAttribute("class", "ui-state-disabled", null);
        }

        if (option.isEscape()) {
            writer.writeText(option.getLabel(), null);
        } else {
            writer.write(option.getLabel());
        }

        writer.endElement("label");
    }

}

Which is registered as follows in faces-config.xml :faces-config.xml注册如下:

<render-kit>
    <renderer>
        <component-family>org.primefaces.component</component-family>
        <renderer-type>org.primefaces.component.SelectManyCheckboxRenderer</renderer-type>
        <renderer-class>com.example.YourSelectManyCheckboxRenderer</renderer-class>
    </renderer>
</render-kit>

Tooltip support for selectManyCheckbox was added since Primefaces v6.2 version.自 Primefaces v6.2 版本起添加了对 selectManyCheckbox 的工具提示支持。 Other components support this too.其他组件也支持这一点。

XHTML code is the same BalusC reported XHTML 代码与 BalusC 报告的相同

<p:selectManyCheckbox layout="pageDirection" value="#{roleBean.selectedRoles}" converter="roleConverter">
    <f:selectItems value="#{roleBean.roles}" var="role" 
        itemValue="#{role}" itemLabel="#{role.name}" itemDescription="#{role.description}" />
</p:selectManyCheckbox>

but is not anymore required override the PrimeFaces SelectManyCheckboxRenderer但不再需要覆盖 PrimeFaces SelectManyCheckboxRenderer

I had to modify BalusC's solution to make it work in my case.我不得不修改 BalusC 的解决方案以使其适用于我的情况。 That is beacause I had to build the List of SelectItems on the Bean-side.那是因为我必须在 Bean 端构建 SelectItem 列表。

public List<SelectItem> getMyItems(){
  List<SelectItem> mySelectItems = new ArrayList<>();
  [loop or ohter code to collect items]
  // create SelectItem with description
  mySelectItems.add(new SelectItem([value], [label], [description]));
  ...
  return mySelectItems;
}

Then these SelectItems can be used with:然后这些 SelectItems 可以用于:

<f:selectItems value="#{myBean.myItems}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM