Primefaces 重置p:树选定值

Primefaces 重置p:树选定值,primefaces,tree,reset,Primefaces,Tree,Reset,我需要重置p:树中的选定值。我在表单中创建了一个重置按钮,将p:tree元素放在其中。该按钮将树的选定值作为空值。在backingbean中,当我按下这个按钮时,值是清晰的。但在界面中,即使我刷新页面,旧的选定值仍被标记。这是我的密码: p:树 <p:tree id="treeClassifier" value="#{navBarController.rootClassifier}" var="node" select

我需要重置p:树中的选定值。我在表单中创建了一个重置按钮,将p:tree元素放在其中。该按钮将树的选定值作为空值。在backingbean中,当我按下这个按钮时,值是清晰的。但在界面中,即使我刷新页面,旧的选定值仍被标记。这是我的密码:

p:树

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

我的代码中有什么错误?

您可以通过下面的示例来实现使用哪种方法resetSelectedNode来刷新值

xhtml


另请参见:

这是我在项目中所做的: 我定义了这个方法,并根据需要调用它

public static void resetAllInputChildren(String parentId) {
    FacesContext currentInstance = FacesContext.getCurrentInstance();
    if (currentInstance != null && currentInstance.getViewRoot() != null) {
        RequestContext.getCurrentInstance().reset(parentId);
    }
}
我通常设置表单id而不是parentId。但这是您的选择,您可以将任何组件id设置为parentId

还有一件事,这不是primefaces问题,我建议的解决方案适用于所有jsf2项目和所有输入组件。

这里是另一个示例

XHTML

领域

import java.io.Serializable;

public class Document implements Serializable, Comparable<Document> {

    private String name;

    private String size;

    private String type;

    public Document(String name, String size, String type) {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //Eclipse Generated hashCode and equals
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((size == null) ? 0 : size.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Document other = (Document) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (size == null) {
            if (other.size != null) {
                return false;
            }
        } else if (!size.equals(other.size)) {
            return false;
        }
        if (type == null) {
            if (other.type != null) {
                return false;
            }
        } else if (!type.equals(other.type)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }

    public int compareTo(Document document) {
        return this.getName().compareTo(document.getName());
    }
}
import java.io.Serializable;
公共类文档实现了可序列化、可比较的{
私有字符串名称;
私有字符串大小;
私有字符串类型;
公共文档(字符串名称、字符串大小、字符串类型){
this.name=名称;
这个。大小=大小;
this.type=type;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getSize(){
返回大小;
}
公共void setSize(字符串大小){
这个。大小=大小;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
//Eclipse生成的hashCode和equals
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((name==null)?0:name.hashCode();
result=prime*result+((size==null)?0:size.hashCode();
result=prime*result+((type==null)?0:type.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
文件其他=(文件)obj;
if(name==null){
if(other.name!=null){
返回false;
}
}如果(!name.equals(other.name)){
返回false;
}
如果(大小==null){
if(other.size!=null){
返回false;
}
}如果(!size.equals(other.size)){
返回false;
}
if(type==null){
if(other.type!=null){
返回false;
}
}如果(!type.equals(other.type)){
返回false;
}
返回true;
}
@凌驾
公共字符串toString(){
返回名称;
}
公共整数比较(文档){
返回这个.getName().compareTo(document.getName());
}
}

有了这些建议,我意识到我需要重置所选数组中的值并更新树中的节点。因此,我在代码中做了以下更改:

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);
    }
}
public void reset(){
....
this.selectedClassifiers=null;
this.rootNode=initTree(tree);
}
公共树节点初始化树(GenericTree树){
GenericTreeNode root=tree.getRoot();
TreeNode rootJSF=新的DefaultTreeNode(“根”,null);
rootJSF.setSelected(false);
for(GenericTreeNode gnt:root.getChildren()){
如果(gnt.getData().getId()!=-1){
TreeNode childTree=newDefaultTreeNode(gnt.getData(),rootJSF);
childTree.setSelected(false);
addChildsToTree(gnt,childTree);
}
}
返回rootJSF;
}
public void addChildsToTree(GenericTreeNode父级,TreeNode父级JSF){
for(GenericTreeNode子项:parent.getChildren()){
TreeNode newNode=newdefaulttreenode(child.getData(),parentJSF);
newNode.setSelected(false);
addChildsToTree(子节点,新节点);
}
}

通过这些更改,我修复了我的代码。

我在具有多个选择的会话bean中尝试了resetSelectedNode函数,并且发生了任何事情:\。你能举一个具体案例的例子吗?
@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
    }
}
public static void resetAllInputChildren(String parentId) {
    FacesContext currentInstance = FacesContext.getCurrentInstance();
    if (currentInstance != null && currentInstance.getViewRoot() != null) {
        RequestContext.getCurrentInstance().reset(parentId);
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <f:facet name="first">
            <meta http-equiv="Content-Type" 
                  content="text/html; charset=UTF-8" />
            <meta name="viewport" 
                  content="user-scalable=no, 
                  width=device-width, 
                  initial-scale=1.0, 
                  maximum-scale=1.0"/>
        </f:facet>

        <title>Tree provides three selection modes, "single", "multiple" and "checkbox".</title>
    </h:head>

    <h:body id="bodyView">
        <h:form>
            <p:growl id="msgs" showDetail="true" escape="false"/>

            <h3 style="margin-top:0">Single</h3>
            <p:tree value="#{treeSelectionView.root1}" 
                    id="simpleSelection"
                    var="doc"
                    selectionMode="single"
                    selection="#{treeSelectionView.selectedNode}">
                <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}" />

            <h3>Multiple with metakey</h3>
            <p:tree id="multipleSelection"
                    value="#{treeSelectionView.root1}" 
                    var="doc"
                    selectionMode="multiple"
                    selection="#{treeSelectionView.selectedNodes1}">
                <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.displaySelectedMultiple(treeSelectionView.selectedNodes1)}"/>
            <p:commandButton value="Reset" 
                             update="multipleSelection" 
                             icon="ui-icon-newwin"
                             process="@this"
                             actionListener="#{treeSelectionView.resetSelectedNode}" />

            <h3>Multiple with Checkbox</h3>
            <p:tree id="checkboxSelection"
                    value="#{treeSelectionView.root1}" 
                    var="doc"
                    selectionMode="checkbox"
                    selection="#{treeSelectionView.selectedNodes2}">
                <p:treeNode icon="ui-icon-note">
                    <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.displaySelectedMultiple(treeSelectionView.selectedNodes2)}"/>
            <p:commandButton value="Reset" 
                             update="checkboxSelection" 
                             icon="ui-icon-newwin"
                             process="@this"
                             actionListener="#{treeSelectionView.resetSelectedNode}" />
        </h:form>
    </h:body>
</html>
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.model.TreeNode;
import org.primefaces.showcase.service.DocumentService;

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

    private TreeNode root1;
    private TreeNode root2;
    private TreeNode root3;
    private TreeNode selectedNode;
    private TreeNode[] selectedNodes1;
    private TreeNode[] selectedNodes2;

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

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

    public TreeNode getRoot1() {
        return root1;
    }

    public TreeNode getRoot2() {
        return root2;
    }

    public TreeNode getRoot3() {
        return root3;
    }

    public TreeNode getSelectedNode() {
        return selectedNode;
    }

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

    public TreeNode[] getSelectedNodes1() {
        return selectedNodes1;
    }

    public void setSelectedNodes1(TreeNode[] selectedNodes1) {
        this.selectedNodes1 = selectedNodes1;
    }

    public TreeNode[] getSelectedNodes2() {
        return selectedNodes2;
    }

    public void setSelectedNodes2(TreeNode[] selectedNodes2) {
        this.selectedNodes2 = selectedNodes2;
    }

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

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

    public void displaySelectedMultiple(TreeNode[] nodes) {
        if (nodes != null && nodes.length > 0) {
            StringBuilder builder = new StringBuilder();

            for (TreeNode node : nodes) {
                builder.append(node.getData().toString());
                builder.append("<br />");
            }

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.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
    }
}
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.primefaces.showcase.domain.Document;

@ManagedBean(name = "documentService")
@ApplicationScoped
public class DocumentService {

    public TreeNode createDocuments() {
        TreeNode root = new DefaultTreeNode(new Document("Files", "-", "Folder"), null);

        TreeNode documents = new DefaultTreeNode(new Document("Documents", "-", "Folder"), root);
        TreeNode pictures = new DefaultTreeNode(new Document("Pictures", "-", "Folder"), root);
        TreeNode movies = new DefaultTreeNode(new Document("Movies", "-", "Folder"), root);

        TreeNode work = new DefaultTreeNode(new Document("Work", "-", "Folder"), documents);
        TreeNode primefaces = new DefaultTreeNode(new Document("PrimeFaces", "-", "Folder"), documents);

        //Documents
        TreeNode expenses = new DefaultTreeNode("document", new Document("Expenses.doc", "30 KB", "Word Document"), work);
        TreeNode resume = new DefaultTreeNode("document", new Document("Resume.doc", "10 KB", "Word Document"), work);
        TreeNode refdoc = new DefaultTreeNode("document", new Document("RefDoc.pages", "40 KB", "Pages Document"), primefaces);

        //Pictures
        TreeNode barca = new DefaultTreeNode("picture", new Document("barcelona.jpg", "30 KB", "JPEG Image"), pictures);
        TreeNode primelogo = new DefaultTreeNode("picture", new Document("logo.jpg", "45 KB", "JPEG Image"), pictures);
        TreeNode optimus = new DefaultTreeNode("picture", new Document("optimusprime.png", "96 KB", "PNG Image"), pictures);

        //Movies
        TreeNode pacino = new DefaultTreeNode(new Document("Al Pacino", "-", "Folder"), movies);
        TreeNode deniro = new DefaultTreeNode(new Document("Robert De Niro", "-", "Folder"), movies);

        TreeNode scarface = new DefaultTreeNode("mp3", new Document("Scarface", "15 GB", "Movie File"), pacino);
        TreeNode carlitosWay = new DefaultTreeNode("mp3", new Document("Carlitos' Way", "24 GB", "Movie File"), pacino);

        TreeNode goodfellas = new DefaultTreeNode("mp3", new Document("Goodfellas", "23 GB", "Movie File"), deniro);
        TreeNode untouchables = new DefaultTreeNode("mp3", new Document("Untouchables", "17 GB", "Movie File"), deniro);

        return root;
    }
}
import java.io.Serializable;

public class Document implements Serializable, Comparable<Document> {

    private String name;

    private String size;

    private String type;

    public Document(String name, String size, String type) {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //Eclipse Generated hashCode and equals
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((size == null) ? 0 : size.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Document other = (Document) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        if (size == null) {
            if (other.size != null) {
                return false;
            }
        } else if (!size.equals(other.size)) {
            return false;
        }
        if (type == null) {
            if (other.type != null) {
                return false;
            }
        } else if (!type.equals(other.type)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return name;
    }

    public int compareTo(Document document) {
        return this.getName().compareTo(document.getName());
    }
}
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);
    }
}