[英]How to insert PrimeFaces p:selectManycheckbox value to database
[英]Primefaces tooltip for p:selectManyCheckbox or other p:selectMany*/One*
我想为p:selectManyCheckBox
每个元素添加一个工具提示。 但是我想不出解决办法。
我有一个Role
类,它有 3 个属性,“id”(Long)、“name”(String)和“description”(String)。 显示名称,我希望将描述作为工具提示。
这是一段工作代码:
<p:selectManyCheckbox layout="pageDirection" value="#{roleBean.selectedRoles}" converter="roleConverter">
<f:selectItems value="#{roleBean.roles}" var="role" itemLabel="#{role.name}" itemValue="#{role}"/>
</p:selectManyCheckbox>
roleConverter
是一个FacesConverter
,它将Role
转换为 id,反之亦然。
我想出了这个:
<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>
但不幸的是它不起作用。
您可以通过使用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>
自 PrimeFaces 6.2 起就支持此功能(实际上是因为您现在正在阅读的这个答案)。
如果您还没有使用 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");
}
}
在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>
自 Primefaces v6.2 版本起添加了对 selectManyCheckbox 的工具提示支持。 其他组件也支持这一点。
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>
但不再需要覆盖 PrimeFaces SelectManyCheckboxRenderer
我不得不修改 BalusC 的解决方案以使其适用于我的情况。 那是因为我必须在 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;
}
然后这些 SelectItems 可以用于:
<f:selectItems value="#{myBean.myItems}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.