2

I was wondering if you could call javascript inside of an ajax statment specificlly I am trying to get the following to work.

        <p:commandLink id="saveButton" value="Save" >
            <p:ajax event="click" actionListener="#{bean.saveButtonPressed()}" />
            <p:ajax event="click" actionListener="if(#{cbean.showSaveOverlay}){saveOverlay.show();}" />
        </p:commandLink>

And showSaveOverly gets set inside saveButtonPressed.

Any idea how I would do this?

1 Answer 1

12

Use the PrimeFaces-provided RequestContext API.

First normalize your ajax listener:

<p:ajax event="click" listener="#{cbean.showSaveOverlay}" />

Then add the script to RequestContext#getScriptsToExecute() in the action listener method accordingly:

public void showSaveOverlay() {
    if (...) {
        RequestContext.getCurrentInstance().getScriptsToExecute().add("saveOverlay.show()");
    }
}

If you're not on PrimeFaces 7.0 yet, then use RequestContext#execute() instead:

public void showSaveOverlay() {
    if (...) {
        RequestContext.getCurrentInstance().execute("saveOverlay.show()");
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

There is no actionListener in p:ajax. I have the last version of the PrimeFaces 6.3
it could be a good idea and great help to say how and where to define saveOverlay as a javaScript code.
@Mazy JavaScript code is normally defined in a JavaScript file.
and how to call it? JS/file/adress/name.function() ? and if in the same package is with name.function() ?
@Mazy: not related to JSF. Start here htmldog.com/guides/javascript/beginner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.