简体   繁体   English

右键单击primefaces treenode时,不会触发select事件

[英]select event doesn't fire when right click on primefaces treenode

I have jsf page : 我有jsf页面:

<p:contextMenu for="treeProfileSetEvent"
                                   nodeType="PROFILESET">
                        <p:menuitem value="#{lang['common.button.add']}" 
                                    icon="ui-icon-plus"
                                    update="@([id$=editPanel_event])"
                                    actionListener="#{profileSetEventController.prepareAdd()}">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" value="#{vpcrfConst.BTN_ADD}" />
                        </p:menuitem>                     
                        <p:menuitem value="#{lang['common.button.delete']}" 
                                    icon="ui-icon-close"
                                    update="@([id$=btnPanel]) @([id$=treeProfileSetEvent]) @([id$=msgInfo])" 
                                    actionListener="#{profileSetEventController.prepareDelete()}"
                                    oncomplete="PF('deleteDialog').show()">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_DELETE}" />                            
                        </p:menuitem>                                          
                    </p:contextMenu>
                    <p:contextMenu for="treeProfileSetEvent"
                                   nodeType="PARENT">   
                        <p:menuitem value="#{lang['common.button.edit']}" 
                                    icon="ui-icon-pencil"                                    
                                    update="@([id$=editPanel_profileSetEvent])"
                                    actionListener="#{profileSetEventController.prepareEdit()}">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_EDIT}" />
                        </p:menuitem>
                        <p:menuitem value="#{lang['common.button.delete']}" 
                                    icon="ui-icon-close"
                                    update="@([id$=btnPanel]) @([id$=treeProfileSetEvent]) @([id$=msgInfo])" 
                                    actionListener="#{profileSetEventController.prepareDelete()}"
                                    oncomplete="PF('deleteDialog').show()">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_DELETE}" />                            
                        </p:menuitem>                                          
                    </p:contextMenu>
                    <p:contextMenu for="treeProfileSetEvent"                                  
                                   nodeType="PARENT_HAS_CHILD">
                        <p:menuitem value="#{lang['common.button.delete']}" 
                                    icon="ui-icon-close"
                                    ajax="true"
                                    update="@([id$=btnPanel]) @([id$=treeProfileSetEvent]) @([id$=msgInfo])" 
                                    actionListener="#{profileSetEventController.prepareDelete()}"
                                    oncomplete="PF('deleteDialog').show()">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_DELETE}" />                            
                        </p:menuitem>                                          
                    </p:contextMenu>
                    <p:contextMenu for="treeProfileSetEvent"
                                   nodeType="CHILD">    
                        <p:menuitem value="#{lang['common.button.edit']}" 
                                    icon="ui-icon-pencil"
                                    update="@([id$=editPanel_profileSetEvent])"
                                    actionListener="#{profileSetEventController.prepareEdit()}">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_EDIT}" />
                        </p:menuitem>
                        <p:menuitem value="#{lang['common.button.delete']}" 
                                    icon="ui-icon-close"
                                    update="@([id$=btnPanel]) @([id$=treeProfileSetEvent]) @([id$=msgInfo])" 
                                    actionListener="#{profileSetEventController.prepareDelete()}"
                                    oncomplete="PF('deleteDialog').show()">
                            <f:setPropertyActionListener target="#{profileSetEventController.formStatus}" 
                                                         value="#{vpcrfConst.BTN_DELETE}" />                            
                        </p:menuitem>                                          
                    </p:contextMenu>
                    <p:tree id="treeProfileSetEvent"
                            value="#{profileSetEventController.root}"
                            selection="#{profileSetEventController.selectedNode}"
                            dynamic="true"                            
                            selectionMode="single"   
                            animate="true"
                            cache="true"
                            var="ps"
                            styleClass="vpcrf-tree-50pc">
                        <p:ajax event="select" listener="#{profileSetEventController.onNodeSelectListener}" update="@([id$=btnPanel]) @([id$=dlgProfileSetEvent])" />
                        <p:ajax event="expand" listener="#{profileSetEventController.onNodeExpandListener}" update="@([id$=btnPanel]) @([id$=dlgProfileSetEvent])" />
                        <p:treeNode id="parentNode"                                     
                                    icon="vpcrf-icon-profile" 
                                    type = "PROFILESET">
                            <h:outputText value="#{ps}" title="#{ps}"
                                          styleClass="vpcrf-txt-400" />
                        </p:treeNode>
                        <p:treeNode id="eventParent" icon="vpcrf-icon-event-parent" type = "PARENT">
                            <h:outputText value="#{ps}" title="#{ps}"
                                          styleClass="vpcrf-txt-400" />
                        </p:treeNode>
                        <p:treeNode id="eventParentHasChild" icon="vpcrf-icon-event-parent" type = "PARENT_HAS_CHILD">
                            <h:outputText value="#{ps}" title="#{ps}"
                                          styleClass="vpcrf-txt-400" />
                        </p:treeNode>
                        <p:treeNode icon="vpcrf-icon-event-child" type = "CHILD">
                            <h:outputText value="#{ps}" title="#{ps}"
                                          styleClass="vpcrf-txt-400" />
                        </p:treeNode>
                    </p:tree>

And the controller : 和控制器:

    public void onNodeSelectListener(NodeSelectEvent e) {
        try {            
            selectedNode = e.getTreeNode();
            if (selectedNode.getData() instanceof ProfileSetDTO) {
                selectedProfileSet = (ProfileSetDTO) selectedNode.getData();
            } else {
                event = (EventDTO) selectedNode.getData();
                System.out.println(event.getEventName());
            }
            profileSet = null;
            formStatus = Const.CLEAR;
        } catch (Exception ex) {
            reportError("msgInfo", "msg.error.unknown");
            logger.error(ex, ex);
        }
    }

The problem is ajax event select only fire when I left-click on treenode, when right-click it doesn't work. 问题是ajax事件选择仅在我左键单击treenode时才触发,而右键单击则不起作用。 Is there any way to make it work when I right-click on node ? 当我右键单击节点时,有什么方法可以使其工作? Any help would be great. 任何帮助都会很棒。

On Primefaces 5.x (I'm using 5.1, but should work also on newer versions), you should be able to solve your issue by adding an ajax event related to contextMenu in the JSF page, as follows: 在Primefaces 5.x(我使用的是5.1,但也应该在较新版本上工作)上,您应该能够通过在JSF页面中添加与contextMenu相关的ajax事件来解决问题,如下所示:

<p:ajax event="contextMenu" listener="#{profileSetEventController.onNodeSelectListener}" update="@([id$=btnPanel]) @([id$=dlgProfileSetEvent])" />

I tend to prefer this solution to the one already posted (the one that "overrides" the Primefaces onRightClick function) for three reasons. 我倾向于使用此解决方案,而不是已经发布的解决方案(“替代” Primefaces onRightClick函数的解决onRightClick ),原因有三点。

First, there is no need to modify the Primefaces javascript (what if a newer version changes something about it? It's likely that, in this case, you have to modify your custom script!) 首先,无需修改Primefaces javascript(如果较新的版本对其有所更改,该怎么办?在这种情况下,您可能必须修改自定义脚本!)

Second, you may want to separate the behavior of the listener onNodeSelectListener depending on which click (left or right) is fired by using e.isContextMenu() in the backing bean, so the controller may become something like: 其次,您可能希望通过在备用Bean中使用e.isContextMenu()e.isContextMenu()触发哪个单击(向左或向右)来分离侦听器onNodeSelectListener的行为,因此控制器可能变为:

public void onNodeSelectListener(NodeSelectEvent e) {
    if (e.isContextMenu()) {
      // right click has been fired
    } else {
      // left click has been fired
    }
}

The second parameter true in this.selectNode(d, true) of the Primefaces script nodeRightClick serves exactly this purpose. Primefaces脚本nodeRightClick this.selectNode(d, true)中的第二个参数true nodeRightClick用于此目的。 In other words, by using this.selectNode(d) (as the overriding script does), it will be not possible to decide whether the listener has been invoked by a left or a right click. 换句话说,通过使用this.selectNode(d) (如覆盖脚本所做的那样),将无法确定是通过左击还是右键单击调用了侦听器。

Last, you may want to use a different listener ( onRightClickSelectListener ) for the two events ( select and contextMenu ) in the JSF page, as follows: 最后,您可能希望对JSF页面中的两个事件( selectcontextMenu )使用不同的侦听器( onRightClickSelectListener ),如下所示:

<p:ajax event="select" listener="#{profileSetEventController.onNodeSelectListener}" update="@([id$=btnPanel]) @([id$=dlgProfileSetEvent])" />
<p:ajax event="contextMenu" listener="#{profileSetEventController.onRightClickSelectListener}" update="@([id$=btnPanel]) @([id$=dlgProfileSetEvent])" />

and consequentely in the backing bean, as: 因此在后备bean中,如下所示:

public void onNodeSelectListener(NodeSelectEvent e) {
   // called when a select event occurs
}

public void onRightClickSelectListener(NodeSelectEvent e) {
  // called when a contextMenu event occurs
}

Therefore using a contextMenu event only for the right click, instead of using select event for both clicks, may be a cleaner approach anyway. 因此,仅对右键单击使用contextMenu event ,而不是对两次单击使用select event ,可能仍然是一种更干净的方法。

Hope that helps. 希望能有所帮助。

I think you are facing the same problem I was having . 我认为您正面临与同样的问题 I still don't get why this is working in the PrimeFaces showcase, but I was able to fix it by replacing PrimeFaces' nodeRightClick handler (JavaScript): 我还是不明白这为什么在PrimeFaces工作展示,但我可以通过更换PrimeFaces'修复它nodeRightClick处理器(JavaScript的):

PrimeFaces.widget.BaseTree.prototype.nodeRightClick = function(e, a) {
    PrimeFaces.clearSelection();
    if ($(e.target).is(":not(.ui-tree-toggler)")) {
        var d = a.parent(), b = a.hasClass("ui-tree-selectable");
        if (b && this.cfg.selectionMode) {
            var c = this.isNodeSelected(d);
            if (!c) {
                if (this.isCheckboxSelection()) {
                    this.toggleCheckboxNode(d)
                } else {
                    this.unselectAllNodes();
                    // Fixed right click selecting
                    // original code: this.selectNode(d, true)
                    this.selectNode(d); // <-- Fix
                }
            }
            this.fireContextMenuEvent(d)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM