我正在尝试在素面中实现动态树结构,其中从root node
开始的用户将能够通过两个{{1来向每个节点中添加siblings
和children
}}。
示例如下:
根
在添加同级后按下
按下添加子项(两次)
在this之后,我实现了以下结构:
tree.xhtml
commandButtons
TreeController
<p:tree id="tree" value="#{treeController.root}"
var="node" dynamic="true" cache="false">
<p:ajax event="expand" listener="#{treeController.onNodeExpand}" />
<p:treeNode>
<h:outputText value="#{node}" />
<p:commandButton style="width:50%; margin: 10px;" value="Add Sibling">
<p:ajax event="click" listener="#{treeController.addSibling(node)}" update="@this">
</p:ajax>
</p:commandButton>
<p:commandButton style="width:50%; margin: 10px;" value="Add Child">
<p:ajax event="click" listener="#{treeController.addChild(node)}" update="@this">
</p:ajax>
</p:commandButton>
</p:treeNode>
</p:tree>
但是,单击按钮时我得到 @ManagedBean(name = "treeController")
@ViewScoped
public class TreeController implements Serializable {
private TreeNode root; //getters && setters ommitted
@PostConstruct
public void buildTree() {
this.root = new DefaultTreeNode("Root",null);
createNode("ROOT",this.root);
}
public void onNodeExpand(NodeExpandEvent event) {
DefaultTreeNode parent = (DefaultTreeNode) event.getTreeNode();
if (parent.getchildCount() == 1 && parent.getchildren().get(0).getData().toString().equals("ROOT")) {
parent.getchildren().remove(0);
createNode(parent.getData().toString()+ "'s Child " + (parent.getchildren().size() + 1),parent);
createNode(parent.getData().toString()+ "'s Child " + (parent.getchildren().size() + 1),parent);
}
}
public void addSibling(TreeNode node){
// I need here the corresponding TreeNode element to add to it's parent a new TreeNode
}
public void addChild(TreeNode node){
// I need here the corresponding TreeNode element to add into it a new child
}
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("ROOT",node);
}
}
,这是由于函数期望接收String参数(节点)而引起的。
有没有办法在按钮的侦听器上检索TreeNode引用?
也许像SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-6) Method not found: TreeController.addSibling(java.lang.String)
这样的NodeEvent吗?
PS::我将NodeExpandEvent
与Primefaces 7.0
一起使用