Search code examples
jquerytablesorter

tablesorter jquery get sum of numeric column after being filtered


I'm developing with tablesorter jquery, my question is:

is there a way to show the sum of numeric column after being filtered?

for example:

+++++++++++
name- money
+++++++++++
alan- 10
alan- 10
alan- 10
alan- 10
john- 10
john- 10
john- 10

sum of money: 70

if i filter for name and just show 'alan' i wanna get this:

+++++++++++
name- money
+++++++++++
alan- 10
alan- 10
alan- 10
alan- 10

sum of money: 40

thanks for your help :)


Solution

  • Try this... here is the HTML from this demo:

    <table class="tablesorter">
        <thead>
            <tr>
                <th>Name</th>
                <th>Money</th>
            </tr>
        </thead>
        <tbody>
            ...
        </tbody>
    
    </table>Sum of Money: <span class="total"></span>
    

    and the necessary script:

    $('table')
        .on('initialized filterEnd', function(){
            var total = 0;
            $(this).find('tbody tr:visible').each(function(){
                // find money in 2nd column
                total += parseFloat( $(this).find('td:eq(1)').text() );
            });
            $('.total').text(total);
        })
        .tablesorter({
            theme: 'blue',
            widgets: ['filter']
        });