Adding nodes programmatically to a rich:tree
dimar1975 Feb 17, 2010 3:47 AMHi all,
I need to develop a rich:tree where the root elements are created at startup and the leaf nodes when you click on the single leaf nodes. In other words I need to add leaf nodes programmatically. Looking at the examples I've added an index.jsp with:
<rich:tree id="tree" style="width:300px" nodeSelectListener="#{simpleTreeBean.processSelection}"
reRender="tree" ajaxSubmitSelection="true"
value="#{simpleTreeBean.treeNode}" var="item" ajaxKeys="#{null}">
</rich:tree>
This is an extract from SimpleTreeBean:
public class SimpleTreeBean {
private TreeNode rootNode = null;
private List<String> selectedNodeChildren = new ArrayList<String>();
. . . . .
private void loadTree() {
rootNode = new TreeNodeImpl();
TreeNodeImpl childNode = new TreeNodeImpl();
childNode.setData(new Pojo("rootNode","root"));
childNode.setParent(rootNode);
rootNode.addChild("1", childNode);
TreeNodeImpl childChildNode1 = new TreeNodeImpl();
childChildNode1.setData(new Pojo("childChildNode1","node1"));
childChildNode1.setParent(childNode);
childNode.addChild("1.1", childChildNode1);
TreeNodeImpl childChildNode2 = new TreeNodeImpl();
childChildNode2.setData(new Pojo("childChildNode2","node2"));
childChildNode2.setParent(childNode);
childNode.addChild("1.2", childChildNode2);
}
public void processSelection(NodeSelectedEvent event) {
HtmlTree tree = (HtmlTree) event.getComponent();
TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
TreeNodeImpl childChildNode2 = new TreeNodeImpl();
childChildNode2.setData(new Pojo("childChildNode2","new node"));
childChildNode2.setParent(currentNode);
currentNode.addChild("2.1", childChildNode1);
}
. . . .
}
When I click on a node, the processSelection is correclty invoked however I cannot see new nodes added, just the leaf nodes is turned into an empty folder....I don't know if it's just a matter of refreshing the rich:tree or my approach is completely wrong (maybe I should use an expand listener or something else).
Any help ?
thanks a lot
Linda