Intercepting a4j request: commandButton

I am developing a web application using A4J, Richfaces. One of my requirements: I need to check if the user has changed any values ​​in the form when he tries to go from the page.

I have save and cancel buttons on the page. I use the button a4j:commandto cancel the function. Clicking the Cancel button should do below things

  • Check if the user has changed anything on the form (I do this using javascript and have a flag if the user has changed any values)
  • Show confirmation window (javascript confirmation with values ​​"Do you really want to discard the changes - YES NO") when the user changes the values ​​of the form
  • If the user says “YES”, submit the form using AJAX (using A4J).

My a4j command button code is as follows

<a4j:commandButton action="MyClass.cancel_action"
    onclick="checkIsPageChanged()"/>

The problem here is that when using it, a4j:commandButtonI cannot call the javascript intermediate function (a function that checks if the user has updated any values ​​and displays a confirmation window) and then sends the request using ajax. I looked at the code generated by JSF and the code is similar (not exact, but syntactic)

<input type="button"
    onclick="checkIsPageChanged();AJAX.submit('')/>

The fact is that when I press the button, it calls only checkIsPageChanged()and does not call AJAX.submit().

Any workaround for this will help me.

Thanks in advance.

+3
source share
2 answers

, :

onclick="if(!isPageChanged()) {return false}" 

false .

+3

a4j:jsFunction a4j:commandButton, checkIsPageChanged() true.

.

<a4j:commandButton action="MyClass.cancel_action"
    onclick="if(checkIsPageChanged()) { cancel(); }"/>
<a4j:jsFunction name="cancel" action="MyClass.cancel_action"/>
+3

Source: https://habr.com/ru/post/1715766/


All Articles