5 Replies Latest reply on Mar 10, 2010 6:22 AM by harut

    Adding nodes programmatically to a rich:tree

      Hi 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

        • 1. Re: Adding nodes programmatically to a rich:tree
          ilya_shaikovsky

          remove

          ajaxKeys="#{null}"

          from tree definition.

          • 2. Re: Adding nodes programmatically to a rich:tree

            >remove

            ajaxKeys="#{null}"

            >from tree definition.

             

            Unfortunately it doesn't work.

            I have tried another approach, using an Ajax button to add new nodes but it still doesn't work......

             

            <rich:tree id="tree" style="width:300px" nodeSelectListener="#{simpleTreeBean.processSelection}"
                            reRender="selectedNode"
                            value="#{simpleTreeBean.treeNode}" var="item" >
            <rich:tree>
            <a4j:commandButton value="submit" action="#{simpleTreeBean.newNodes}" reRender="tree"></a4j:commandButton>

             

            This is the core section of simpleTreeBean:

             

            public class SimpleTreeBean {
               
                private TreeNode rootNode = null;
                private List<String> selectedNodeChildren = new ArrayList<String>();   
               
                private String nodeTitle;

             

                . . . . . .
                private void addNodes(String path, TreeNode node, Properties properties) {
                    boolean end = false;
                    int counter = 1;
                   
                    while (!end) {
                        String key = path != null ? path + '.' + counter : String.valueOf(counter);

             

                        String value = properties.getProperty(key);
                        if (value != null) {
                            TreeNodeImpl nodeImpl = new TreeNodeImpl();
                            nodeImpl.setData(value);
                            node.addChild(new Integer(counter), nodeImpl);
                            addNodes(key, nodeImpl, properties);
                            counter++;
                        } else {
                            end = true;
                        }
                    }
                }
               
                private void loadTree() {

                    try {
                        Properties properties = new Properties();
                        properties.put("1","AAA");
                        properties.put("1.1","AAA 1.1");
                        properties.put("1.1.1","AAA 1.2");
                      
                        properties.put("2","BBB");
                        properties.put("2.1","BBB 2.1");
                        properties.put("2.1.1","BBB 2.2");
                       
                        rootNode = new TreeNodeImpl();
                        addNodes(null, rootNode, properties);
                       
                    } catch ( Exception e) {
                        throw new FacesException(e.getMessage(), e);
                    } 
                }
                public void newNodes() {
                        Properties properties = new Properties();
                        properties.put("3","CCC");
                        properties.put("3.1","CCC 3.1");
                        properties.put("3.1.1","CCC 3.2");
                          
                        addNodes(null, rootNode, properties);
                   
                }
             

             

               
            }

             


            The method newNodes gets fired but actually no nodes are added to the tree. Please can anybody help me, I'm struggling with this problem since many days.......

            Thanks

            Ermes

            • 3. Re: Adding nodes programmatically to a rich:tree
              nbelaevski

              Hi Ermes,

               

              There is a coding problem in your addNodes(...) function, it doesn't really add nodes when newNodes() is called.

              • 4. Re: Adding nodes programmatically to a rich:tree

                Hi Nick,

                thanks for replying. Well the function addNodes is actually used in the loadTree method to populate initially the rich:tree.

                The tree is correclty loaded at startup....but I cannot add nodes afterwards, when I click the ajax button.....:-(

                • 5. Re: Adding nodes programmatically to a rich:tree
                  harut

                  why the value of the rich:tree is "#{simpleTreeBean.treeNode}"  ???

                  Based on your code it should be "#{simpleTreeBean.rootNode}"

                  (ofcourse if you didn't have such a getter method -- "getTreeNode { return rootNode; }")