-
1. Re: Keeping component state with rich:tree RF 4.1.0
saifeldeen Mar 23, 2012 2:24 AM (in response to saifeldeen)Ok, the answer seems to be quite simple. Leading on from the tree node example in the JSF showcase
1. The xhtml:
{code}
<h:form>
<rich:tree
id="tree"
nodeType="#{node.type}"
var="node"
value="#{treeBean.rootNodes}"
toggleType="ajax"
selectionType="ajax"
toggleListener="#{programTreeBean.processTreeToggle}"
selectionChangeListener="#{treeBean.selectionChanged}">
<rich:treeNode type="country" expanded="#{node.expanded}">
#{node.name}
</rich:treeNode>
<rich:treeNode type="company" icon="/images/tree/disc.gif">
#{node.name}
</rich:treeNode>
<rich:treeNode type="cd" icon="/images/tree/song.gif">
#{node.artist} - #{node.name} - #{node.year}
</rich:treeNode>
</rich:tree>
</h:form>
{code}
2. The NamedNode class needs to implement a property for "expand":
{code}
private boolean expanded;
public boolean isExpanded()
{
return expanded;
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
{code}
3. Set the TreeBean to be @SessionScoped
4. In the TreeBean, implement the TreeToggleListener interface and add the following code
{code}
@Override
public void processTreeToggle(TreeToggleEvent selectionChangeEvent) throws AbortProcessingException
{
// considering only single selection
UITree tree = (UITree) selectionChangeEvent.getSource();
NamedNode node = (NamedNode) tree.getRowData();
node.setExpanded(tree.isExpanded());
}
{code}
And voila, the tree is persisted in the session correctly.