Return from select brings previous value - jQuery

1

I have a table #table and I would like every time I make a filter by select it adds the amount that is in column 6 eq (5) . With the code below it is running, however it displays the previous value that I select. For example, you are displaying 3 lines with 5, 5, and 5 in each of them. When I change option from select to any other option, then it will show me the sum, 25 on the console.

 $('select').on('change', function() {
    var qtd = 0
    $("#tabela tbody tr:not(.filtered)").each(function(){
        qtd += parseInt($(this).find('td').eq(5).html())
    });
    console.log(qtd);
});

What event could I use to calculate it when I change the selection of <select> . I had thought of the obvious, the change .

I'm a beginner with jQuery, so if the code can be written in a better way, feel free to suggest it.

Thank you.

    
asked by anonymous 30.01.2018 / 00:14

2 answers

1

The Tablesorter Filter Widget plugin has a trigger which is fired after the end of the filter.

Then put the calculations inside trigger :

$("#tabela").on("filterEnd",function() {
   var qtd = 0
   $("#tabela tbody tr:not(.filtered)").each(function(){
      qtd += parseInt($(this).find('td').eq(5).html())
   });
   console.log(qtd);
});

Place after plugin initialization:

$("#tabela").tablesorter();
    
30.01.2018 / 01:14
0

I was able to do this:

$(document).ready(function() {
    var qtd = 0;

    $('select').on('blur', function(e) {  
        qtd = 0;
        $("#tabela tbody tr:not(.filtered)").each(function() {
            qtd += parseInt($(this).find('td').eq(5).html());  
        });
    });

    $('select').on('change', function() {       
        setTimeout (function() { 
            $("#tabela thead tr td input[type=search]").eq(0).focus(); 
            $('#total').html(qtd);
        }, 300);
    }); 
});

I do not know if this is the best way to do this if you are following good practices. If someone has a more correct form, please share.

@dvd thanks for your help.

Hugs.

    
30.01.2018 / 13:12