I've an object, say Users, which has 10 properties, I want to bind it to h:selectManyCheckbox and want to display two specific fields of it as itemLabel & itemValue of f:selectItems. How can I do that? Please help!
Asked
Active
Viewed 7,582 times
1 Answers
3
Use the following in your facelet:
<h:selectManyCheckbox id="yourElementID"
value="#{myBean.selectedList}">
<f:converter converterId="userConverter"/>
<f:selectItems value="#{myBean.availableItemsList}"
var="item"
itemLabel="#{item.labelAttribute}"
itemValue="#{item.valueAttribute}"/>
</h:selectManyCheckbox>
Replace:
myBeanwith your bean's nameselectedListwith the list that holds the selected valuesavailableItemsListwith the list of
your items availablelabelAttributewith the attribute's
name that you intend to use as item
label.valueAttributewith the attribute's name that you intend to use as item value
Notice that a converter is referenced in the f:converter element. A h:selectManyCheckbox returns Strings as value. So you need a converter for your User class that converts objects to strings and vice versa. You can implement it as inner class of your managed bean or as separate class.
@FacesConverter(value="userConverter")
public static class UserConverter implements Converter {
public Object getAsObject(FacesContext facesContext,
UIComponent component, String value) {
// your code to convert String to Object
}
public String getAsString(FacesContext facesContext,
UIComponent component, Object object) {
// your code to convert Object to String
}
Alexandre Lavoie
- 8,711
- 3
- 31
- 72
Matt Handy
- 29,855
- 2
- 89
- 112