Call method in bean if condition is true

Asked

Viewed 264 times

0

I created this confirm:

<script type="text/javascript" language="javascript">
            function salvarAntes(){
            var r=confirm("Deseja confirmar?");
                if(r){
                    return true;
                } else {
                    return false;
                }
            }
            </script>

And call in my commandButton:

<p:commandButton value="Confirma" process="@form" onclick="if(salvarAntes() #{bean.salvar});"/>

The idea is to call the method in the bean only if my javascript is true. The problem is that in onclick you say you can’t find the method. Does anyone know how I can solve?

1 answer

0

You can use the Primefaces component <p:remoteCommand> to do this.

Example:

<p:remoteCommand name="rc" actionListener="#{bean.salvar}"/>

And in your boot Victor would do:

<p:commandButton value="Confirma" process="@form" onclick="salvarAntes()"/>

And Voce can call your remoteCommand with javascript like this:

<script type="text/javascript" language="javascript">
      function salvarAntes(){
        var r = confirm("Deseja confirmar?");
            if(r){
                rc(); //make a remote call
                return true;
            } else {
                return false;
            }
      }
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.