[Solved] Reset p:tree selected values

UI Components for JSF
Post Reply
tech4
Posts: 18
Joined: 12 Aug 2014, 00:41

15 Oct 2014, 21:58

I need to reset the selected values in a p:tree. I create a reset button in the form where i put my p:tree element. That button put the selected values of the tree as null values. In the backing bean the values are clear when i press this button. But in the interface even when I refresh the page the old selected value still marked. Here is my code:

p:tree

Code: Select all


<p:tree id="treeClassifier" 
        value="#{navBarController.rootClassifier}" 
        var="node"          
        selectionMode="checkbox" 
        selection="#{navBarController.selectedClassifiers}" 
        style="height: 100px;width: 280px; margin-bottom: 0px; overflow: auto">
      <p:treeNode expandedIcon="ui-icon-folder-open" 
                  collapsedIcon="ui-icon-folder-collapsed">
         <h:outputText value="#{node.description}(#{node.code})"/> 
      </p:treeNode> 
</p:tree>

Create the tree:

Code: Select all


public TreeNode initTree(GenericTree<Classifier> tree) {
        GenericTreeNode<Classifier> root = tree.getRoot();
        TreeNode rootJSF = new DefaultTreeNode("root", null);
        for (GenericTreeNode<Classifier> gnt : root.getChildren()) {
            if (gnt.getData().getId() != -1) {
                TreeNode childTree = new DefaultTreeNode(gnt.getData(), rootJSF);
                //rootJSF.getChildren().add(childTree);
                //f_aux(gnt, rootJSF);
                addChildsToTree(gnt, childTree);
            }
        }
        return rootJSF;
    }

    public void addChildsToTree(GenericTreeNode<Classifier> parent, TreeNode parentJSF) {
        for (GenericTreeNode<Classifier> child : parent.getChildren()) {
            TreeNode newNode = new DefaultTreeNode(child.getData(), parentJSF);
            //parentJSF.getChildren().add(newNode);
            addChildsToTree(child, newNode);
        }
    }

The reset function:

Code: Select all


public void reset() {
       ....
       this.selectedClassifiers = null;
}

What is wrong in my code?
Last edited by tech4 on 15 Nov 2014, 17:20, edited 2 times in total.
Primefaces 5 Glassfish

User avatar
andyba
Expert Member
Posts: 2473
Joined: 31 Mar 2011, 16:27
Location: Steinfeld, near Bremen/Osnabrück, DE
Contact:

16 Oct 2014, 11:13

You need to unselect the all selected TreeNodes and update the p:tree component. The selectedNodes array is only there for your information not to control what is and isn't selected.
EDIT: I am actually getting reacquainted with p:tree...

If you are dealing with selection yourself you can ignore the selectedNode array completely or you can manage it yourself using all the ajax events available.
Be warned: you still need to supply a setSelectedNodes(TreeNode[] selectedNodes) method BUT you should not change the state of the array in your backing bean from it.
This is why you need to manage select and unselect events yourself ie. you are taking over the selection management yourself and in total.
PF 4.x (Elite versions), PF 5, Pf 5.1, PF 6.0
Glassfish 4.1, Mojarra 2.x, Java 8, Payara 4.1.1.
If you haven't read the forum rules read them now

tech4
Posts: 18
Joined: 12 Aug 2014, 00:41

21 Oct 2014, 00:13

Thanks for the answer. I updated the tree, unselecting the selected nodes, and the problem is solved.
Primefaces 5 Glassfish

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 35 guests