12 Replies Latest reply on Oct 20, 2010 4:54 AM by barbaraboie

    Remebering tree expanded state

    mail.micke

      Hi all.

      This is a question which has been asked a lot, I've spent this morning reading lots of posts about this but didn't find one which explained everything.

      I'm hoping to get a clear answer here and if I do, I'll add a nice example to the wiki cookbook :)

      So the question is how do I make sure that the tree expanded/selection state is preserved across request, both redirected and not? (I (svn) checked out the tree-demo sample and had a look, it contains a lot of stuff but couldn't quite detect what was specific to the expanded state)

      I was hoping to have an application scoped bean containing the applications navigation trees (the actual treenode data) and then have a session scoped user specific bean containing a users current tree node selection.
      Is this possible, currently I'm thinking that the tree data needs to be stored for each users and not just the current selection.
      This may be the reason I'm not getting it to work (haven't put the tree data in session scope yet).

      Many thanks,
      Mike

        • 1. Re: Remebering tree expanded state
          mail.micke

          Forgot to mention that I want it working with client switchType.

          • 2. Re: Remebering tree expanded state

            I'm very interested in this too.

            • 3. Re: Remebering tree expanded state
              mail.micke

              I still haven't solved this completely.

              When using componentState attribute with TreeState class the state is saved when using ajax type tree. But with client and server switch type it doesn't work.

              Any pointers greatly appreciated.

              - Mike

              • 4. Re: Remebering tree expanded state
                mail.micke

                This is the last ping,
                then I'll just assume this isn't possible currently.

                A simple anwser like "it is easy, read the docs" will suffice ;)

                • 5. Re: Remebering tree expanded state
                  pankaj.ghosh

                  I am sort of stuck in the same scenario.
                  Does anyone made any breakthroughs in this?

                  Thanks
                  Pankaj

                  • 6. Re: Remebering tree expanded state

                    Hmm...it seems to me, only people who want this feature read this thread ;-)

                    I still don't know how to do this - until someone explains how to do it on client side I do it on server side. With a lot of self written code because I didn't get the "normal way" (explained in the docs) working.
                    Still another reason for asking about a smart and stylish way to do it on client side :-)

                    • 7. Re: Remebering tree expanded state
                      pankaj.ghosh

                      Can you post a code snippet of how to go about it from server side?

                      • 8. Re: Remebering tree expanded state

                        Yes, I can :-)

                        First I use jsFunction to define to methods for collapsing/expanding a selected node (incl. its subtree):

                         <a4j:jsFunction name="expandNode" action="#{ctrl.expandTreeNav}" ajaxSingle="true" reRender="TreeNavigator">
                         <a4j:actionparam name="node" assignTo="#{ctrl.currentNode}"/>
                         </a4j:jsFunction>
                         <a4j:jsFunction name="collapseNode" action="#{ctrl.collapseTreeNav}" ajaxSingle="true" reRender="TreeNavigator">
                         <a4j:actionparam name="node" assignTo="#{ctrl.currentNode}"/>
                         </a4j:jsFunction>
                        


                        These methods I call with a self written context menu. I hope we can use the richfaces context menu for that soon.

                        Further I use a binding in my rich:tree

                        <rich:tree value="#{ctrl.treeNavData}" var="item" nodeFace="#{item.type}" showConnectingLines="true" switchType="ajax" binding="#{ctrl.treeNavBinding}">
                        ...
                        </rich:tree>
                        


                        The java code is the complicated part: First I use on initial tree building a recursive method to set a ListRowKey to each node, to identify them later. Second we need the java methods we call with the jsFunctions. One looks like this:

                        public void expandTreeNav()
                         {
                         TreeState state = (TreeState) treeNavBinding.getComponentState();
                         TreeNavigationNode node = (TreeNavigationNode) getDescendant(treeNavData,
                         currentNode);
                         try
                         {
                         recursiveExpandFromAncestor(state, node);
                         }
                         catch (IOException e)
                         {
                         // TODO When there's a concept do a proper exception handling here.
                         e.printStackTrace();
                         }
                         }
                        


                        The used methods here are (as you'll guess) another recursive methods to navigate through the tree.

                        I have warned you: Lots of self written code.

                        • 9. Re: Remebering tree expanded state
                          mail.micke

                          Hi

                          fmarwede, have you tried using the componentState attribute and bind that to a TreeState object on the backing bean(session scoped) ?

                          That works for me when the switchType is ajax for the tree.

                          But still no solution for client or server side switchType's ...

                          Cheers

                          • 10. Re: Remebering tree expanded state

                            Exact like this.

                            • 11. Re: Remebering tree expanded state
                              seammer

                              In tree2 there is treeState and treeStateBase can solve such problem with very few code.
                              However, I couldn't find the similar part as treeStateBase in richfaces...

                              • 12. Re: Remebering tree expanded state
                                barbaraboie

                                Hello

                                I have the same problem. Is there in meantime a solution to keep the state of a tree with switchType="client"?

                                 

                                Barbara