Sum column values, but only selected rows

Sum column values, but only selected rows

sanzwebsanzweb Posts: 2Questions: 0Answers: 0

Hi guys.
i need to solve this problem. please help me.

This code work fine and sum a column (9) with visible data in the page.:

$.fn.dataTable.Api.register( 'column().data().sum()', function () {
return this.reduce( function (a, b) {
var x = parseFloat( a ) || 0;
var y = parseFloat( b ) || 0;
return x + y;
} );
} );

$('<button class="dt-button">Sum visible hours</button>')
.prependTo('#somma_ore')
.on( 'click', function () {
alert( 'Le ore visibili sono: '+ table.column(9, {page:'current'} ).data().sum() );
} );

});

Now, i need another button to show, always in the same column, only selected rows.

thanks for the suggestions.

Replies

  • kthorngrenkthorngren Posts: 21,154Questions: 26Answers: 4,919

    I think you are looking for the selector-modifier of {selected:true}. The docs have examples.

    Kevin

  • sanzwebsanzweb Posts: 2Questions: 0Answers: 0

    Thank you kthorngren...

    i've tried something like the code below, but don't work.....

    $('<button class="dt-button">Somma Ore selezionate</button>')
    .prependTo('#somma_ore')
    .on( 'click', function () {
    var lines = table.rows( {selected:true} ).data();
    alert( 'Le ore selezionate sono: '+ table.column(9, lines, {page:'current'} ).data().sum());
    });

    because the modifier selected:true must be referred to the rows....not colums.

    I don't know..... :(

  • allanallan Posts: 63,179Questions: 1Answers: 10,410 Site admin

    Your table.column(...) call looks odd. There isn't a signature that would match that in the documentation for column().

    Instead use:

    table.column(9, {selected: true}).data().sum()
    

    Allan

This discussion has been closed.