jsf - I need to click the button twice to make the calculation -


i have method calculates charge need click button twice calculate

  1. this method fetches data in database , calculates

    public float findtarifabandeira1() {     try {         //sql     } catch (noresultexception ne) {         system.out.print("tarifa não encontrada"); // colocar messagesfaces     }      tarifaband1 = (bandeira.getvalorinicial()  + ( bandeira.gettarifacorrida().getvalorkm() * km));     bigdecimal vt = new bigdecimal(tarifaband1).setscale(2, roundingmode.half_even);     tarifaband1 = vt.floatvalue();     return tarifaband1; } 
  2. this method fires click of button calls other methods , method above have method calculates charge need click button twice calculate

    public void consultarbandeira() throws parseexception {     getdata();     gethora();     convertdistance();     findtarifabandeira1();     findtarifabandeira2();     findtarifavolumesdemao ();     findtarifavolumesnormais ();     findtarifavolumesgrandes(); } 
  3. in jsf page use remote command call above method

    <p:commandbutton value="#{msg['btn-search']}"                  styleclass="btn btn-primary" onclick="findroute()"/>  <p:remotecommand actionlistener="#{searchbean.consultarbandeira()}"                  id="consultavalor" name="consultavalor"                  update="bandeira1, bandeira2, nomevpequenos,                          descricaovpequenos, valorvpequenos, nomevnormais,                          descricaovnormais, valorvnormais, nomevgrandes,                          descricaovgrandes, valorvgrandes"/>     

    findroute() function calls javascript consultavalor component triggers method in searchbean.

    function findroute(){     event.preventdefault();      var addressorigin = origin;     var addressdestination = document.getelementbyid("addr").value +                                 document.getelementbyid("city").value;      var request = {         origin: addressorigin,         destination: addressdestination,         travelmode: google.maps.travelmode.driving,         unitsystem: google.maps.unitsystem.metric      };      directionsservice.route(request, function(response, status) {         if (status == google.maps.directionsstatus.ok) {             directionsdisplay.setdirections(response);             directionsdisplay.setpanel(document.getelementbyid("route"));             var route = response.routes[0];             (var = 0; < route.legs.length; i++){                 routesegment = + 1;                 distance = route.legs[i].distance.value;             }             document.getelementbyid("distance").value = distance;         }     });      consultavalor(); }    

when click commandbutton, postback server twice: once remotecommand , once button itself. cause of problem.

since you're using button client-side action only, instruct commandbutton not postback via onclick.

also, since you're setting form data in findroute() javascript method, you'll need make sure processed on backend. default value of process remotecommand @this.

<p:commandbutton value="#{msg['btn-search']}"              styleclass="btn btn-primary" onclick="findroute(); return false"/> <p:remotecommand actionlistener="#{searchbean.consultarbandeira()}"              id="consultavalor" name="consultavalor"              process="@form"              update="bandeira1, bandeira2, nomevpequenos,                      descricaovpequenos, valorvpequenos, nomevnormais,                      descricaovnormais, valorvnormais, nomevgrandes,                      descricaovgrandes, valorvgrandes"/>     

but if me, i'd simplify combining logic commandbutton itself:

<p:commandbutton value="#{msg['btn-search']}"              styleclass="btn btn-primary" onstart="findroute()"              action="#{searchbean.consultarbandeira()}"              process="@form"              update="bandeira1, bandeira2, nomevpequenos,                      descricaovpequenos, valorvpequenos, nomevnormais,                      descricaovnormais, valorvnormais, nomevgrandes,                      descricaovgrandes, valorvgrandes" />     

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -