Sum the data of one column but filter by slected rows

Sum the data of one column but filter by slected rows

sparhsparh Posts: 11Questions: 2Answers: 0

Hi,
i would like to Sum the data of one column but filter by slected rows (https://datatables.net/extensions/select/)

I use this code to get the total of one column and It is working fine:

pageTotal = api
                .column( 3, { page: 'all'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Update footer
            $( api.column( 3 ).footer() ).html(
                formatCurrency(pageTotal)
            );

But nowI would like to get the total of the 3rd column but only selected rows
I have tried this without any success

 pageTotal = api
        .rows({ selected: true })
                .column( 3, { page: 'all'} )
                .data()
                .reduce( function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0 );
 
            // Update footer
            $( api.column( 3 ).footer() ).html(
                formatCurrency(pageTotal)
            ); 

Can you help me with this ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,805Questions: 1Answers: 10,119 Site admin
    Answer ✓

    You would need to do something like:

    var rows = api.rows( { selected: true } ).indexes();
    var data = api.cells( rows, 3, { page: 'all' } );
    ...
    

    Allan

  • sparhsparh Posts: 11Questions: 2Answers: 0

    Thank you Allan

This discussion has been closed.