2 Replies Latest reply on Oct 2, 2009 5:58 AM by Ilya Shaikovsky

    Problem with dynamic add nodes with rich:tree

    Chen Xi Newbie

      Currently I am developing dynamic tree menu. Due to the large data, I can only load and display the first level data. Only when user expand the second level of the node, back end service will fetch data back. Now the problem is I have already add the data to the TreeNode object, however the UI does not render the new nodes to the tree.

      Any one can help me to solve the problem?

      I am using richfaces-api-3.3.1GA & richfaces-impl-3.3.1 GA

      Here is my code

      Xhtml:

      <f:view>
       <a4j:form>
       <!--<a4j:region renderRegionOnly="true" id="ajaxRegionId">-->
       <a4j:outputPanel ajaxRendered="true">
       <rich:panel>
       <rich:tree id="clientTree" value="#{clientTreeController.clients}" var="item"
       reRender="NodeId" ajaxSubmitSelection="true" switchType="ajax"
       treeNodeVar="TreeNodeVar" ajaxKeys="#{null}">
       <rich:treeNode id="treeNodeId" iconLeaf="/img/Transpusers.png"
       icon="/img/Transpusers.png"
       nodeSelectListener="#{clientTreeController.viewUsers}">
       <h:outputText id="NodeId" value="#{item}"/>
       </rich:treeNode>
       </rich:tree>
       </rich:panel>
       </a4j:outputPanel>
       <!--</a4j:region>-->
       </a4j:form>
      </f:view>


      ClientTreeController.java

      @SuppressWarnings({"unchecked"})
       public void viewUsers(NodeExpandedEvent evt) throws IOException {
       System.out.println("Execute viewUsers method");
       Object source = evt.getSource();
       if (source instanceof HtmlTreeNode) {
       UITree tree = ((HtmlTreeNode) source).getUITree();
       if (tree == null) {
       return;
       }
       Object rowKey = tree.getRowKey();
       TreeNode selectedTreeModelNode = tree.getModelTreeNode(rowKey);
      
       if (null != selectedTreeModelNode) {
       System.out.println("The selected node has child [" + selectedTreeModelNode.getChildren().hasNext()+"]");
       String companyName = (String) selectedTreeModelNode.getData();
      
       Client client = clientManager.getClientByName(companyName);
      
       List<UserEntityData> users = client.getUsers();
       if (users == null) return;
      
       for (int k = 0; k < users.size(); k++) {
       TreeNodeImpl userNode = new TreeNodeImpl();
      
       userNode.setData(users.get(k).getUserName());
       userNode.setParent(selectedTreeModelNode);
       selectedTreeModelNode.addChild(k, userNode);
       }
       } else {
       System.out.println("selectedTreeNode is null");
       }
       }
       }