Column sum based on a value from other column

Column sum based on a value from other column

stefutz101stefutz101 Posts: 5Questions: 1Answers: 0
edited August 2018 in Free community support

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! :smile:

Replies

  • kthorngrenkthorngren Posts: 20,342Questions: 26Answers: 4,776

    Looks like you need to update the column-selector in the cells() API:

                    .cells( function ( index, data, node ) {
                        return api.row( index ).data().status === 'accepted' ?
                            true : false;
                    }, 0 ) .  <<< change this to 6
    

    You have it pointing to column 0.

    And also how I can render some dollar signs before each value

    The second example here shows how:
    https://datatables.net/manual/data/renderers

    Kevin

  • stefutz101stefutz101 Posts: 5Questions: 1Answers: 0
    edited August 2018

    Thanks, @kthorngren.

    Meanwhile, I've also found this :

    var total = api
              .column(6)
              .data()
              .reduce(function (a, b) {
                    var cur_index = api.column(6).data().indexOf(b);
                    if (api.column(6).data()[cur_index] != "accepted") {
                    return intVal(a) + intVal(b);
                  }
                  else { return intVal(a); } // I assume this can be deleted
              }, 0) 
    

    From https://stackoverflow.com/questions/42180812/datatables-footercallback-conditional-on-another-cell
    I also managed to add curency too. Thanks! :smile:

  • jimbo_sjimbo_s Posts: 3Questions: 2Answers: 0

    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!

  • maurettomauretto Posts: 1Questions: 0Answers: 0
    edited November 2019

    data().indexOf(...)

    in a big dataset is very slow.
    I suggest you to use instead:

    .reduce(function(a, b, idx) {
       if (api.column(6, {search: 'applied'}).data()[idx] != 'accepted' {
          return a + intVal(b);
       } else {
          return a;
       }
    }, 0) 
    
This discussion has been closed.