0

I have a PrimeFaces website with a left hand side menu. The menu has several menuitems:

<p:menuitem 
value="#{msgs['text.menuitemMessage']}" 
action="#{miClass.menuitemAction}" 
styleClass="mnt-icon icon_24 #{miClass.getStyle('parameter')}" 
global="false" />

The menuItem gets made nicely with an onclick value that comes from the miClass.menuitemAction method. Is it possible to overwrite the PrimeFaces onclick function in javascript ? Like in a very specific setting on only one page. A check is performed when a menuItem is clicked and if the check return true the usual onclick action is performed, if the check is false the onclick is overwritten with return false.

  • What about `onclick="yourJS()"` or `onclick="yourRemoteCommand()"`? – Vasil Lukach Aug 23 '18 at 14:53
  • And the source is open and questions about overriding a function of one specific component widget already exist in stackoverflow https://stackoverflow.com/questions/39639532/override-a-method-from-a-primefaces-specific-widget – Kukeltje Aug 23 '18 at 18:12
  • @VasilLukach That's the problem, `onclick="return false;"` doesn't stop the menuItem. A highly unstandard behavior. – ForguesR Jun 04 '21 at 14:18

1 Answers1

0

Thanks for the comments, I got my answer based off of them. The solution was to get the menuitem handle and rewrite the onclick function:

for (var i = 0; i < menuitemsLength; i++) {
            var menuitemFunction = PF("widget_j_idt87_j_idt88").menuitemLinks[i].onclick;

            PF("widget_j_idt87_j_idt88").menuitemLinks[i].onclick = function () {
                globalFunctionForLeaving = $(this).attr('onclick');
                console.log(globalFunctionForLeaving);

                if ("#{miClass.somethingDone}") {
                    PF('popupWindow').show();
                    return false;
                } else {
                    menuitemFunction;
                }
            }
        }

The script is only run on a certain page.