You can achieve this by using the SelectItem#getDescription()
property as below:
<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).
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:
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
:
<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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…