Enable button when the last request is executed successfully

1

I have a table with multiple records with their respective information and ids . When I click on a button I make a request via post and Ajax individually from that row of the table.

But I have another button that scrolls the entire table by taking their ids and doing the requests individually.

When you have finished all the requests, you have or have not enabled a button to continue.

The problem is when to capture the last request of these records. I can not.

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 records and only enable the button when the last request is completed and it is true .

Someone helps me ...

    
asked by anonymous 08.01.2018 / 03:25

2 answers

1

In your function you can pass another value

function calcularNotas(aluno, numero) {

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

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

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

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

ultimo_numero = 10;

If the table is mounted dynamically and you do not have control of how many students can come, then at assembly time you set the value of this 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 that number in the function, just as you pass the student:

calcularNotas(aluno, numero);
    
08.01.2018 / 13:07
1

I was able to use the hint of JulioHenrique97 with an adaptation.

At the time we do not remember but then the light goes on.

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: there is no way to die! huashuahsuhasu Valew JulioHenrique97 !!

    
08.01.2018 / 20:03