调用上下文菜单操作时未设置选定的树节点 [英] Selected tree node not set when context menu action is invoked
问题描述
我正在使用上下文菜单创建PrimeFaces(5.3)树.选定的节点应存储在#{myBean.selectedNode}
中.当我使用鼠标左键选择一个节点时,将设置正确的节点.但是,当我尝试从上下文菜单中对节点执行操作时,没有先选择它,则未设置正确的节点(未调用bean中的setter).
I'm creating a PrimeFaces (5.3) tree with a context menu. Selected nodes should be stored in #{myBean.selectedNode}
. When I select a node using the left mouse button the correct node is set. But, when I try to run an action on a node from a context menu, without selecting it first, the correct node isn't set (the setter in my bean is not called).
我正在遵循PrimeFaces展示柜中的示例,然后我想我已经把所有东西都整理好了.我在做什么错了?
I'm following the example in the PrimeFaces showcase, and I suppose I've got everything lined up. What am I doing wrong?
如您所见,在PrimeFaces展示柜中,您可以立即右键单击一个节点,单击查看",然后咆哮声将显示正确的节点.
As you can see, in the PrimeFaces showcase you are able to immediately right click a node, click "View", and the growl will display the correct node.
我认为bean代码不相关(它是 ViewScoped
,并且有一个 private TreeNode selectedNode
,带有getter和setter).
I don't think the bean code is relevant (it is ViewScoped
and there is a private TreeNode selectedNode
with getter and setter).
不过,这里有一些有趣的地方:
Here are the interesting bits though:
public void onNodeSelect(NodeSelectEvent event) {
MyTreeNode myTreeNode = (MyTreeNode) event.getTreeNode();
myController.setSelected(myTreeNode.getEntity());
}
public void addChild(String name) {
MyTreeNode myTreeNode = (MyTreeNode) selectedNode;
MyTreeNode childNode = myTreeNode.addChild(name);
myController.setSelected(childNode.getEntity());
myController.insert();
}
XHTML
<h:form id="mainForm">
<p:tree value="#{myBean.root}" var="node"
id="myTree" dynamic="true"
selectionMode="single" selection="#{myBean.selectedNode}">
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed"
type="myType">
<h:outputText value="#{node}"/>
</p:treeNode>
<p:ajax event="select" listener="#{myBean.onNodeSelect}" />
</p:tree>
<p:contextMenu for="myTree">
<p:menuitem action="#{myBean.addChild('new')}"
value="Add"
process="@this"
update=":mainForm:myTree"/>
</p:contextMenu>
</h:form>
推荐答案
事实证明,您需要未公开的 contextMenu
Ajax事件侦听器.
It turns out you need the undocumented contextMenu
Ajax event listener.
请参阅: