我想在primeface树中添加和删除节点。
树是动态填充的,即当你展开它时会创建子节点。
现在我想通过弹出窗口添加和删除节点,(当我们右键单击节点时,它会提供添加或删除选项)。如果我们选择add,则会打开一个弹出窗口,我们可以选择填充节点的详细信息。如果我们单击保存节点,它将添加到所选节点下的树中。
但是,在下面的代码中以及在弹出对话框中填充数据之后,未添加节点并且树扩展不起作用。
有任何建议如何解决这个问题?
package com.test.primefaces;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
@ManagedBean
@SessionScoped
public class TreeController implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private TreeNode root;
private String newNodeName;
private TreeNode selectedNode;
@PostConstruct
public void buildTree() {
root = new DefaultTreeNode("Root", null);
createNode("Node 0", root);
createNode("Node 1", root);
createNode("Node 2", root);
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
/* public void addChildNode(ActionEvent actionEvent) {
System.out.println("Selected Node: "+getSelectedNode().toString());
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);
}*/
public void addChildNode() {
TreeNode newNode = new DefaultTreeNode(newNodeName, getSelectedNode());
System.out.println("Selected Node: "+getSelectedNode().toString());
/*TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);*/
}
public void deleteNode() {
TreeNode newNode = new DefaultTreeNode(newNodeName, getSelectedNode());
/*System.out.println("Selected Node: "+getSelectedNode().toString());
TreeNode newNode = new DefaultTreeNode("Node New", getSelectedNode());
getSelectedNode().setExpanded(true);*/
}
public void onNodeExpand(NodeExpandEvent event) {
DefaultTreeNode parent = (DefaultTreeNode) event.getTreeNode();
if (parent.getChildCount() == 1 && parent.getChildren().get(0).getData().toString().equals("DUMMY")) {
parent.getChildren().remove(0);
createNode("Node A", parent);
createNode("Node B", parent);
createNode("Node C", parent);
}
}
private void createNode(String tag, TreeNode parent) {
TreeNode node = new DefaultTreeNode(tag, parent);
// Create Dummy node, just to make the parent node expandable
new DefaultTreeNode("DUMMY", node);
}
public String getNewNodeName() {
return newNodeName;
}
public void setNewNodeName(String newNodeName) {
this.newNodeName = newNodeName;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public TreeNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(TreeNode selectedNode) {
this.selectedNode = selectedNode;
}
}
和xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<script name="jquery/jquery.js" library="primefaces"></script>
</h:head>
<h:form id="form">
<p:tree id="tree" value="#{treeController.root}" var="node"
dynamic="true" cache="false" selectionMode="single"
selection="#{treeController.selectedNode}">
<p:ajax event="expand" listener="#{treeController.onNodeExpand}" />
<p:treeNode>
<h:outputText value="#{node}" />
</p:treeNode>
</p:tree>
<p:contextMenu for="tree">
<p:menuitem value="Add Node" update="treeAdd" icon="ui-icon-search"
oncomplete="PF('treeAddDialog').show()" />
<p:menuitem value="Delete" update="tree" icon="ui-icon-close"
actionListener="#{treeController.deleteNode}" />
</p:contextMenu>
<p:dialog header="tree node" widgetVar="treeAddDialog" modal="true"
showEffect="fade" hideEffect="fade" resizable="false">
<p:outputPanel id="treeAdd" style="text-align:center;">
<p:panelGrid columns="2" columnClasses="label,value">
<f:facet name="header">
<p:graphicImage
name="demo/images/car/#{dtContextMenuView.selectedCar.brand}-big.gif" />
</f:facet>
<h:outputLabel for="nodeName" value="NodeName" />
<p:inputText id="nodeName" value="#{treeController.newNodeName}"
required="true" label="nodename" />
<f:facet name="footer">
<p:commandButton actionListener="#{treeController.addChildNode}"
value="Save" oncomplete="treeAddDialog.hide();" />
<p:commandButton value="Cancel" onclick="treeAddDialog.hide();" />
</f:facet>
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>
</html>