1 Reply Latest reply on Mar 23, 2012 2:24 AM by saifeldeen

    Keeping component state with rich:tree RF 4.1.0

    saifeldeen

      Hi there,

       

      I'm not quite sure on how to keep component state with rich:tree using v4.1.0 final.  My situation is this: I have a tree that users may expand / collapse items at any point during their session.  However, when a page is refreshed (e.g. new request), the tree is collapsed again.  I would like the tree to maintain state throughout the users' session.

       

      I have read a number of other discussions, particularly https://community.jboss.org/thread/161388 which appears to "resolve" the problem, however, it appears to be a (necessary) hack as the RF source code needs direct modification

       

      In the above issue Nick Belaevski suggests "In RF4 you should bind 'expanded' and 'selection' to model beans explicitly using EL-expressions.", however, there doesn't appear to be  "expanded" and "selection" attributes known by the models / libraries.

       

      Can someone please provide some guidance or even better, a solution, on how to maintain the state of a rich:tree using v4.1.0 final?

       

      Kind regards,

      sh

        • 1. Re: Keeping component state with rich:tree RF 4.1.0
          saifeldeen

          Ok, the answer seems to be quite simple.  Leading on from the tree node example in the JSF showcase

           

          1. The xhtml:

          {code}

          <h:form>

              <rich:tree

               id="tree"

               nodeType="#{node.type}"

               var="node"

               value="#{treeBean.rootNodes}"

               toggleType="ajax"

               selectionType="ajax"

               toggleListener="#{programTreeBean.processTreeToggle}"

               selectionChangeListener="#{treeBean.selectionChanged}">

                  <rich:treeNode type="country" expanded="#{node.expanded}">

                      #{node.name}

                  </rich:treeNode>

                  <rich:treeNode type="company" icon="/images/tree/disc.gif">

                      #{node.name}

                  </rich:treeNode>

                  <rich:treeNode type="cd" icon="/images/tree/song.gif">

                      #{node.artist} - #{node.name} - #{node.year}

                  </rich:treeNode>

              </rich:tree>

          </h:form>

          {code}

           

           

          2. The NamedNode class needs to implement a property for "expand":

           

           

          {code}

          private boolean expanded;

          public boolean isExpanded()

          {

              return expanded;

          }

           

          public void setExpanded(boolean expanded)

          {

              this.expanded = expanded;

          }

          {code}

           

          3. Set the TreeBean to be @SessionScoped

           

          4. In the TreeBean, implement the TreeToggleListener interface and add the following code

           

          {code}

          @Override

          public void processTreeToggle(TreeToggleEvent selectionChangeEvent) throws AbortProcessingException

          {

              // considering only single selection

              UITree tree = (UITree) selectionChangeEvent.getSource();

              NamedNode node = (NamedNode) tree.getRowData();

              node.setExpanded(tree.isExpanded());

          }

          {code}

           

          And voila, the tree is persisted in the session correctly.