Conditional total sum in certain column in footer

Conditional total sum in certain column in footer

bumbarbumbar Posts: 2Questions: 0Answers: 0

I want to take the sum from a column, but only from cells that have a td class="active"
I put this class with conditions in the code.
I can also put this class in tr class="active".
I would also like things to be calculated in footerCallback to recalculate the sum of the column when doing a search

<table id="example" class="display" style="width:100%">
        <tbody>
            <tr>
                <td>Edinburgh</td>
                <td class="active">30</td>
            </tr>
            <tr>
                <td>Tokyo</td>
                <td>170</td>
            </tr>
            <tr>
                <td>Ashton</td>
                <td>86</td>
            </tr>
            <tr>
                <td>Edinburgh</td>
                <td class="active">40</td>
            </tr>
    </table>

SUM: 70

    "footerCallback": ...........
            var api = this.api(), data;

            var cells = $('td.active');  <-- get only active cells

            pageTotal = api
                .column( 4, { page: 'current'} )
                .data()
                .reduce( function (a, b) {

                //if (api.cells(cells).data()) { <-- This is not right and ugly!!!
                    return intVal(a) + intVal(b);
                //}

                }, 0 );

            $( api.column( 4 ).footer() ).html(
                '$'+pageTotal +' ( $'+ total +' total)'
            );

Can anyone give advice? Thank you.

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This example should help. It's adding a class "red" to all age cells > 60, then summing those on the current page. To sum all pages, remove "{page: current}". Hope that helps,

    Colin

  • bumbarbumbar Posts: 2Questions: 0Answers: 0

    This is what I wanted. Thanks!

Sign In or Register to comment.