Call method in bean if condition is true

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 I call 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 the my javascript is true. The problem is that onclick says that it does not find the method. Does anyone know how I can resolve?

    
asked by anonymous 29.05.2018 / 17:29

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 button you would do:

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

And you 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>
    
29.05.2018 / 19:04