Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset p:tree selected values

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

<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:

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:

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

What is wrong in my code?

like image 619
tech4 Avatar asked Oct 15 '14 11:10

tech4


2 Answers

You can achieve by below example which method resetSelectedNode is used to fresh values.

xhtml

<h:form>
    <p:growl id="msgs" showDetail="true" escape="false"/>
    <p:inputText value="#{treeSelectionView.input}" />
    <h3 style="margin-top:0">Single</h3>
    <p:tree value="#{treeSelectionView.root1}" 
            id="simpleSelection"
            var="doc"
            selectionMode="single"
            selection="#{treeSelectionView.selectedNode}"
            dynamic="true">
        <p:treeNode expandedIcon="ui-icon-folder-open" 
                    collapsedIcon="ui-icon-folder-collapsed">
            <h:outputText value="#{doc.name}"/>
        </p:treeNode>
        <p:treeNode type="document" icon="ui-icon-document">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>
        <p:treeNode type="picture" icon="ui-icon-image">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>
        <p:treeNode type="mp3" icon="ui-icon-video">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>
    </p:tree>

    <p:commandButton value="Display" 
                     update="msgs" 
                     icon="ui-icon-newwin"
                     actionListener="#{treeSelectionView.displaySelectedSingle}"/>
    <p:commandButton value="Reset" 
                     update="simpleSelection" 
                     icon="ui-icon-newwin"
                     process="@this"
                     actionListener="#{treeSelectionView.resetSelectedNode}" />
</h:form>

managedbean

@ManagedBean(name = "treeSelectionView")
@ViewScoped
public class SelectionView implements Serializable {

    private TreeNode root1;
    private TreeNode selectedNode;
    private String input;

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }

    @ManagedProperty("#{documentService}")
    private DocumentService service;

    @PostConstruct
    public void init() {
        root1 = service.createDocuments();
    }

    public TreeNode getRoot1() {
        return root1;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

    public void setSelectedNode(TreeNode selectedNode) {
        this.selectedNode = selectedNode;
    }

    public void setService(DocumentService service) {
        this.service = service;
    }

    public void displaySelectedSingle() {
        System.out.println("input: " + input);
        if (selectedNode != null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    public void resetSelectedNode(ActionEvent event) {
        FacesContext context = FacesContext.getCurrentInstance();
        Application application = context.getApplication();
        ViewHandler viewHandler = application.getViewHandler();
        UIViewRoot viewRoot = viewHandler.createView(context, context.getViewRoot().getViewId());
        context.setViewRoot(viewRoot);
        context.renderResponse(); //Optional
    }
}

See also: Clear Input Components

like image 154
wittakarn Avatar answered Nov 19 '22 02:11

wittakarn


With these suggestions i realized that i need to reset the values from the selected array and update the nodes in the tree. So I made the following changes in my code:

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

    public TreeNode initTree(GenericTree<Classifier> tree) {
       GenericTreeNode<Classifier> root = tree.getRoot();
       TreeNode rootJSF = new DefaultTreeNode("root", null);
       rootJSF.setSelected(false);
       for (GenericTreeNode<Classifier> gnt : root.getChildren()) {
         if (gnt.getData().getId() != -1) {
            TreeNode childTree = new DefaultTreeNode(gnt.getData(), rootJSF);
            childTree.setSelected(false);
            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);
        newNode.setSelected(false);
        addChildsToTree(child, newNode);
    }
}

With these changes I fix my code.

like image 2
tech4 Avatar answered Nov 19 '22 01:11

tech4