Enable button when last request is executed successfully

Asked

Viewed 53 times

1

Personal I am wanting to enable a button only when the last post request via Ajax is executed successfully.

I have a table with several records with their respective information and ids. By clicking a button I make a request via post and individual Ajax of this table row.

But I have another button that runs through the entire table picking up their respective ids and making the requests individually.

When finishing all requests is that enable or not a button to continue.

The problem is when to capture the last request of these records. I’m not getting.

My code:

function calculaMedia(aluno) {
   $.when( calculaMediaFuncao(aluno, function(){}) ).then(function(ret){       
      console.log(ret);
      console.log( "Disparei assim que as requisições se completaram" );
      return true;
   }).fail(function(){
      console.log( "Disparei se uma ou mais requisições falharam" );
      return false;
   });
}

function calculaMediaFuncao(aluno, callback) {
   $.post( "/index.php?r=aluno%2Fnota-aluno&aluno="+aluno+"&disc="+disc+"&tipo="+tipo+"&ap="+notaAP, function( data ) {
      $( "#situacaoFinal_"+aluno ).html( data );
      callback( true );
   }).fail(function() {
   callback( false );
   })
}

function calcularNotas() {
   $("#btnGravarNotas").removeAttr("disabled");
   $('#tabelaAlunos > tbody  > tr').each(function(key, data) {
      var aluno = $(data).attr('data-aluno-id');
      console.log(aluno);
      calculaMediaFuncao(aluno, function (res) {
         if (!res) {
            $("#btnGravarNotas").attr("disabled", "disabled");                                  
         }
      });
   });
}

Individually I call for calculaMedia(aluno).

But when you click a button you have to call all the records and only enable the button when the last request is completed and is true.

Somebody help me...

  • You’ll get better answers if you give people code they can use to reproduce the problem.

2 answers

1

in its function you can pass another value

function calcularNotas(aluno, numero) {

and make a query if that number is equal to the last number

if(numero == ultimo_numero){
   $('#botao_que_eu_quero_habilitar').prop('disabled', false);
}

But how do you know the last number? will depend on how your table is being mounted, static or dynamic. if it is static is easy for example:

the table contains fixed 10 students and this does not change, so just assign:

ultimo_numero = 10;

if the table is mounted dynamically and you have no control of how many students can come, then at assembly time you set the value of that variable in the last table item, for example:

//possivelmente um for que é usado pra montar dinamicamente sua tabela
for(i = 0; i < lista.lenght(); i++){

   //aqui a verificação se é o ultimo item
   if(i+1==lista.lenght()){
     numero = i;
   }

}

and then you pass this number on the function, just like you pass the student:

calcularNotas(aluno, numero);
  • I thought that too. The table is dynamic but there is a way to get the last number. The "problem" is that it should only be enabled if there was no error in entering the data. As I said, the function goes through all the rows of the table doing some checks, as values are inserted for each record of the table. In the callback of the function calculatMediaFuncao() if no error returns true, otherwise it will return false. So if at some point, when going through the records of the table, this check fails, the continue button should not be enabled.

  • Poxa @Joséarijunior then Voce asked the wrong question 'last request is completed and for true. But in this case it is also possible, uses a global variable like this return and leaves an if not to change it if it is filled as false, if it has false vc not enabled, if it has true vc enable

  • True!! Next time I will formulate the question directly! ;-D

1

I managed using the tip of Juliohenrique97 with an adaptation.

At the time we do not remember but then the little light lights up.. rsrsrs

function calcularNotas() {
    var ultAluno = "";
    var habilita = true;
    $(\'#tabelaAlunos > tbody  > tr\').each(function(key, data) {
        ultAluno = $(data).attr(\'data-aluno-id\');
    });
    $(\'#tabelaAlunos > tbody  > tr\').each(function(key, data) {
        var aluno = $(data).attr(\'data-aluno-id\');
        calculaMediaFuncao(aluno, function (res) {
            if (!res) {
                habilita = false;
            }
            if (aluno === ultAluno && habilita) { 
                $("#btnGravarNotas").prop("disabled", false);
            }
        });
    });
}

As I always say: only there is no way to die! huashuahsuhasu Valew Juliohenrique97!!

  • hahaha nothing, already were lights for me many times too

Browser other questions tagged

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