Primefaces右键单击 - 支持bean方法调用

时间:2015-09-14 19:56:35

标签: jsf-2 primefaces contextmenu right-click backing-beans

有没有办法通过右键单击图像来调用JSF2 / Primefaces 5.x中的支持bean ajax方法? Primefaces中有一个contextMenu组件,但它会显示一个我不想要的菜单。

2 个答案:

答案 0 :(得分:4)

使用this jQuery代码和PrimeFaces <p:remoteCommand>可以实现这一点,这里是简单的代码:

<h:form>
        <p:graphicImage id="myImage" onmousedown="rmc(event)" library="img" name="myImage.png" class="RMC"/>
        <p:remoteCommand name="rightMouseClick" action="#{backingBean.method}" update="myImage"/>
</h:form>
    <script>
        $(document).on("mousedown", ".RMC", function () {
            $(".RMC").each(function () {
                this.oncontextmenu = function () {
                    return false;
                };
            })

            $(".RMC").mousedown(function (e) {
                if (e.button == 2) {
                    rightMouseClick();
                    return false;
                }
                return true;
            });
        });

        function rmc(e) {
            this.oncontextmenu = function () {
                return false;
            };

            if (e.button == 2) {
                rightMouseClick();
                return false;
            }
            return true;
        }
    </script>

它将禁用类RMC的图像上的鼠标右键菜单,并从辅助bean调用方法。如果您不想要停用该菜单,只需删除.each()部分。

编辑: 更新DOM元素后,它会丢失$(document).ready()期间发出的事件,因此您必须直接在组件中添加onmousedown事件。

EDIT2: 更改$(document).ready(),现在即使在更新后也适用于具有RMC类的元素。

答案 1 :(得分:0)

很简单......打破你的问题。首先考虑html。您只需通过jquery添加右键单击并使用@Override public View getView(int position, View convertView, ViewGroup parent){ View v = convertView; // .... ImageButton notificationsButton = (ImageButton) v.findViewById(R.id.fragment_student_home_notification_imageButton); notificationsButton.setTag(R.id.fragment_student_home_notification_imageButton); // .... return v; } @Override public void onClick(View v) { if(v!=null && v.getTag()!=null && v.getTag()==R.id.fragment_student_home_notification_imageButton){ // do something you like } } 调用后备

相关问题