Column sum based on a value from other column
Column sum based on a value from other column

Hello.
In the following table, I'm trying to sum up every 'buyer earnings' that have an 'accepted' status, I don't know how and why but it ended up summing( I think ) the id column.
$('#reviews').DataTable( {
'footerCallback': function ( row, data, start, end, display ) {
var api = this.api(), data;
// Remove the formatting to get integer data for summation
var intVal = function ( i ) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '')*1 :
typeof i === 'number' ?
i : 0;
};
// Total over all pages
total = api
.cells( function ( index, data, node ) {
return api.row( index ).data().status === 'accepted' ?
true : false;
}, 0 )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Total over this page
pageTotal = api
.column( 6, { page: 'current'} )
.data()
.reduce( function (a, b) {
return intVal(a) + intVal(b);
}, 0 );
// Update footer
$( api.column( 6 ).footer() ).html(
'$'+pageTotal +' ( $'+ total +' total)'
);
}
} );
/// rest of the code
And also how I can render some dollar signs before each value.
I tried something like this:{ data: 'status', render: '$'.status } - I know it's dumb and it's so wrong, but I have no idea how to get that value.
Thanks for your time!
This discussion has been closed.
Replies
Looks like you need to update the
column-selector
in thecells()
API:You have it pointing to column 0.
The second example here shows how:
https://datatables.net/manual/data/renderers
Kevin
Thanks, @kthorngren.
Meanwhile, I've also found this :
From https://stackoverflow.com/questions/42180812/datatables-footercallback-conditional-on-another-cell
I also managed to add curency too. Thanks!
Hey, can you please explain what this does? I think it's exactly what I need but i'm not sure how to manipulate it
Thanks!
in a big dataset is very slow.
I suggest you to use instead: