问题
I am using a Richfaces' picklist and I want to populate the right-side panel with a list of SelectItems from my backing bean.
Populating the left-side is not a problem from the backing bean, however, the right hand side is problematic.
This is what I currently have
<h:outputText value="Roles" />
<rich:pickList showButtonsLabel="false">
<f:selectItems value="#{Bean.allRoles}" />
</rich:pickList>
EDIT:
So I have roles 'a', 'b', 'c', and 'd'.
The user has roles 'a' and 'd', so 'a' and 'd' should be on the right-side panel and 'b' and 'c' should be on the left-side panel.
EDIT:
Further explanation.
I have three lists for the user.
- All posible roles (a thru d)
- All roles the user is part of (a and d)
- All roles the user is NOT part of (b and c)
All lists have the data type ArrayList<SelectItem>
.
I need the capability to move individual roles between list number 1 and list number 2 and then save the new set of roles. I thought the picklist would be the best richfaces object for the job.
回答1:
You want this code:
<h:outputText value="Roles" />
<rich:pickList showButtonsLabel="false" value="#{bean.chosenRoles}">
<f:selectItems value="#{Bean.allRoles}" />
</rich:pickList>
and in your bean you want:
private String[] chosenRoles;
+ getter/setter
Whenver you want to make default roles you just add the roles into the chosenRoles array (for example in the bean constructor). That way chosenRoles will always contain the elements on the right side of the picklist, while elements on the left side are not in the array.
Hope this helps!
回答2:
It seems that I found the solution.
<rich:pickList value="#{aBean.rightSideValues}">
<f:selectItems value="#{aBean.leftSideValues}"/>
</rich:pickList>
"#{aBean.rightSideValues}"
should point to the list or array of objects. With these values right side of the pick list will be populated.#{aBean.leftSideValues}
should point to the list of the SelectItem object.
ONE NOTICE -- SelectItem object MUST BE constructed with objects from the "#{aBean.rightSideValues}"
.
Example.
class ABean{
...
List<SomeObject> getRightSideValues(){
...
}
List<SelectItem> getLeftSideValues(){
List<SomeObjects> someObjects = getAllAvailableObjects();
List<SelectItem> sItems = new ArrayList<SelectItem>();
for(SomeObject sObj : someObjects){
SelectItem sItem = new SelectItem(sObj, sObj.toString());
sItems.add(sItem);
}
return sItems;
}
Notice that SelectItem takes first argument and this argument is the reference to the SomeObject. In the internals rich faces will compare objects from the "#{aBean.rightSideValues}"
with objects from the
#{aBean.leftSideValues}
with help of the method
SomeObject.equals()
回答3:
If you need values other than text, you need to make a converter.
See http://programmingnutsandbolts.blogspot.com/2009/08/richfaces-picklist-converters.html
回答4:
As in RF 4.3.3 and probably earlier, too, you also need to override the hash method of your model class (entity bean). So, only overriding the equals method for your converter is not enough. The following worked out for me and my PubThread entity bean.
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (!(obj instanceof PubThread))
return false;
PubThread objectToCheck = (PubThread) obj;
boolean isShallowCopy = (this.id.equals(objectToCheck.id));
return isShallowCopy;
}
@Override
public int hashCode() {
return (this.id.hashCode());
}
回答5:
Set the value of the pickList to an array of the value of the SelectItem.
Whatever you start out with on the right hand side will then basically just be the default value of the pickList:
<rich:pickList value="#{aBean.rightSideValues}">
<f:selectItems value="#{aBean.leftSideValues}"/>
</rich:pickList>
来源:https://stackoverflow.com/questions/839893/how-to-populate-the-right-side-of-a-richfaces-picklist