9 Replies Latest reply on Jan 16, 2014 5:22 AM by Marko Jurisic

    rich:tree  expand all TreeNodes

    jak wei Newbie

      I use the code :

      <rich:tree switchType="client" style="width:300px" value="#{mapbean.treeBean.data}" var="item" nodeFace="#{item.type}"
       rowKeyVar="d" toggleOnClick="true">
       <rich:treeNode type="type" iconLeaf="../images/ico.gif" icon="../images/ico.gif">
       <h:outputText value="#{item.type}" />
       </rich:treeNode>


      how can be the all TreeNodes expanded when the page loaded complete

      please help me !!

        • 1. Re: rich:tree  expand all TreeNodes
          Florian Marwede Expert

          There are more than one possibilities I think.

          First I tried to use a component binding for the whole tree and then the method queueExpandAll(). But here I got an exception (no parent) I don't understand. Perhaps somebody else can help here.

          Second I tried a method binding with the adviseNodeOpened attribute and this works. Here a little bit of code:

          In your jsp/xhtml:

           <rich:tree ... adviseNodeOpened="#{bean.nodeOpened}" ... >
           ...
           </rich:tree>
          


          In your bean:

           public Boolean nodeOpened(UITree tree) {
           if (expandAll) {
           return Boolean.TRUE
           }
           return null;
           }
          


          Now you only have to make sure that the boolean 'expandAll' is initially set to true and after loading the page set to false. Otherwise your tree stays completely open whatever you click.

          • 2. Re: rich:tree  expand all TreeNodes
            Marcin Domagala Newbie

            Hi know that its late answer but you can also do it on client side

            function collapseAll() {
             var tree = window['Richfaces_Tree_tree_tree'];
             var childs = tree.childs;
             collapse(childs);
            }
            
            function expandAll(){
             var tree = window['Richfaces_Tree_tree_tree'];
             var childs = tree.childs;
             expand(childs);
            }
            
            function collapse(childs) {
             for (var i = 0; i < childs.length; i++) {
             var element = childs;
             var cc = element.childs;
             if (cc.length != 0) {
             if (!element.isCollapsed()){
             collapse(cc);
             element.eventCollapsionClick();
             }
             }
             }
             }
            
             function expand(childs) {
             for (var i = 0; i < childs.length; i++) {
             var element = childs;
             var cc = element.childs;
             if (cc.length != 0) {
             if (element.isCollapsed){
             element.eventCollapsionClick();
             expand(cc);
             }
             }
             }
            }
            

            Just change the name of tree (probably somehow you could get it from varState)

            • 3. Re: rich:tree  expand all TreeNodes
              Benno Markiewicz Newbie

              You can use

              UITree.queueExpandAll()
              from java code.

              See http://labs.jboss.com/auth/wiki/en/ExpandCollapseTreeNodes

              • 4. Re: rich:tree  expand all TreeNodes
                vivian hu Novice

                Is there any way to make the tree expand at the first time it renders? With out use .js or going back to server to call UITree.queueExpandAll()?

                • 5. Re: rich:tree  expand all TreeNodes
                  Benno Markiewicz Newbie

                  @vh: The code from fmarwede (second post) should do the job.

                  • 6. Re: rich:tree  expand all TreeNodes
                    Nick Belaevski Master

                    Please see the similar problem solved in richfaces-demo (see recursiveTreeNodesAdaptor demo). PhaseListener is used there.

                    • 7. Re: rich:tree  expand all TreeNodes
                      Paul Pantages Novice

                      Hello Forum,

                      I am using richfaces packaged with Seam 2.0.1.GA

                      I also want to expand a tree the first time it renders. My needs are slightly different: I keep my tree in SESSION scope and it can be shared by several pages if the operator
                      is inclined to open up a few tabs.

                      The "postback check" in the richfaces demo will re-apply the policy on my tree whenever a new tab is opened, which I don't really want.

                      I have also found that tree.getParent() returns null the first time the tree is bound to my backing bean. This prevents me from calling queueExpandAll() as the latter throws an illegalArgument exception when the parent is null. The binding occurs before TreeStateAdvisor.adviseNodeOpened() is called. I noticed that the parent is no longer null when adviseNodeOpened() is called.

                      But in subsequent bindings, the parent is not null. Even if the user re-refreshed pages, or opens tabs etc.

                      So, my question is, this behaviour (the null value returned by getParent(), first time ) normal/expected, or a bug? I want to use this to control my TreeStateAdvisor, but am a bit hesitant to rely on such a thing....

                      I can set a flag in my TreeStateAdvisor, to tell it not to do the expansion once the tree has been established. This seems to work OK.

                      My tree is setup like so:

                       <rich:tree switchType="ajax" stateAdvisor="#{subnetTreeImpl.subnetTreeStateAdvisor}" binding="#{subnetTreeImpl.nodeTableTree}">
                      
                       <rich:recursiveTreeNodesAdaptor roots="#{treeData}" var="node" nodes="#{node.children}" >
                      ....
                      

                      My TreeStateAdvisor
                      package centina.sa.client.utils;
                      
                      import org.richfaces.component.UITree;
                      
                      import org.richfaces.component.state.TreeStateAdvisor;
                      
                      import org.richfaces.model.TreeRowKey;
                      
                      // Based on the examples from richfaces demo org/richfaces/treemodeladaptor
                      
                      public class SubnetTreeStateAdvisor implements TreeStateAdvisor {
                      
                       public SubnetTreeStateAdvisor()
                       {
                       this.depth = 1 ;
                       this.isTreeBound = false;
                       }
                      
                       public Boolean adviseNodeOpened(UITree tree) {
                      
                       if ( ! this.isTreeBound )
                       {
                       Object key = tree.getRowKey();
                       TreeRowKey treeRowKey = (TreeRowKey) key;
                      
                       if (treeRowKey == null ) {
                       return Boolean.TRUE;
                       }
                       else if (treeRowKey.depth() <= this.depth) {
                       return Boolean.TRUE;
                       }
                       }
                       return null;
                       }
                      
                       public Boolean adviseNodeSelected(UITree tree) {
                       return null;
                       }
                      
                       // Set the initial expansion depth of the tree.
                       // Set to 0 to collapse.
                      
                       public int depth;
                      
                       public int getDepth() {
                       return this.depth;
                       }
                      
                       public void setDepth( int depth ) {
                       this.depth = depth;
                       }
                      
                       // This flag is used to disable the initial "advice"
                       // once the subnetTree has been bound. Thus we don't
                       // override the operator's discretion if he collapses the tree
                       // after its initial rendering.
                      
                       // Normally, opening a new tab or re-rendering the page using
                       // the top menu would cause the tree to be re-expanded up to the
                       // "depth" setting.
                      
                       private boolean isTreeBound;
                      
                       public boolean getIsTreeBound() {
                       return this.isTreeBound;
                       }
                      
                       public void setIsTreeBound( boolean isTreeBound ) {
                       this.isTreeBound = isTreeBound;
                       }
                      
                      }
                      


                      • 8. Re: rich:tree  expand all TreeNodes
                        temujin tharves Newbie

                        I am using richfaces-components-ui-4.2.2.Final version. which dodn't contain TreeStateAdvisor class, How can i make default expand tree.

                        • 9. Re: rich:tree  expand all TreeNodes
                          Marko Jurisic Newbie

                          Hi,

                           

                          <rich:treeNode expanded="#{true}"> has done the trick for me.

                           

                          Maybe it saves few minutes for somebody else.

                           

                          Regards,

                           

                          Marko