Rich:tree getData() for selection question
andygibson Dec 28, 2007 10:23 AMHey folks,
I know there are a number of posts on rich:tree, but couldn't find anything to answer the problem. I cannot get data from tree nodes passed to events to get the data for the selected node. I don't know whether it can be done using the page side tree definition, or whether I have to go through the process of defining the tree model on the server side by a model object implementing the TreeNode interface?
I have a rich:tree, and use the recursive tree nodes adapter to get my nodes in the tree. My tree is displaying fine and it works nicely.
I have a set of security categories which are defined recursively (a category has a parent category) and each category has none or more security rights. I have the following in my xhtml file :
<rich:tree switchType="client" reRender="tabUsers" ajaxSubmitSelection="true" >
<rich:recursiveTreeNodesAdaptor nodes="#{v_node.categories}"
roots="#{securityCategories}" var="v_node">
<rich:treeNode iconLeaf="/img/iconFolder.gif">
<h:outputText value="#{v_node.description}"></h:outputText>
</rich:treeNode>
<rich:treeNodesAdaptor nodes="#{v_node.rights}" var="v_rightNode">
<rich:treeNode iconLeaf="/img/padlock.gif" nodeSelectListener="#{userRightAssignment.userRightNodeSelected}">
<h:outputText value="#{v_rightNode.description}" />
</rich:treeNode>
</rich:treeNodesAdaptor>
</rich:recursiveTreeNodesAdaptor>
</rich:tree>
This displays just fine and my select listener "userRightNodeSelected" is defined as :
public void userRightNodeSelected(NodeSelectedEvent event) {
UITreeNode node = null;
node = (UITreeNode) event.getComponent();
if (node == null) {
return;
}
Object selectedData = node.getData();
if (selectedData instanceof UserRight) {
log.debug("Setting User Right");
setSelectedUserRight((UserRight) selectedData);
} else {
log.debug("Nulling User Right");
setSelectedUserRight(null);
}
log.debug("User right is now #0", selectedUserRight);
}
The event is called and the node obtained is an HtmlTreeNode. When I call getData(), I get null every time.
The question is how do I attach data (the Category or UserRight) to the tree node and can it be done automatically with using recursive tree nodes adapter, or do I have to create the tree based on TreeNode classes using the Model?
Cheers,
Andy