Subtotal Group by Class

Subtotal Group by Class

obworleyobworley Posts: 7Questions: 2Answers: 0

Is it possible find a subtotal on a group by class? I need to only subtotal certain columns, but want the same instance of DataTables to work on multiple reports. I've found some examples, but they all name the column that needs to be subtotaled.

Answers

  • obworleyobworley Posts: 7Questions: 2Answers: 0

    I figured out a work around. It's not elegant, but I'll leave it here for anyone else who is trying to dynamically sum by group (I put the totals below the group). It also detects if the subtotal should be a number or a currency and formats accordingly.

    Sum will show a subtotal, dol will format the subtotal as currency. The "sum" and/or "dol" classes are on the td cells in the tbody. I use the same detection method for the totals in the footer.

    I used a number of different references and it took two days so I I'm not sure who all helped me. Thank you to all of you!

        "drawCallback": function ( settings ) {
            var api = this.api();
            var rows = api.rows( {page:'current'} ).nodes();
            var last=null;
            var colnos = api.row(0).data().length;
            var total = new Array();
            var groupid = -1;
            var subtotal = new Array();
            var sumArray = new Array();
            var dolArray = new Array();
    
            api.columns({ page: 'current' }).every(function () {
                var thisCol = api.column(this).index();
                var sumClass = api.column(this).nodes().to$().hasClass('sum');
                sumArray[thisCol] = sumClass;
    
                var dolClass = api.column(this).nodes().to$().hasClass('dol');
                dolArray[thisCol] = dolClass;
            });
    
            api.column(0, {page:'current'} ).data().each( function ( group, i ) {
                if ( last !== group ) {
                    groupid++;
                    $(rows).eq( i ).after(
                        '<tr class="group"><td align=right style="background-color:wheat;">Sub Total</td></tr>'
                    );
                    last = group;
                }
    
                val = api.row(api.row($(rows).eq( i )).index()).data();
                $.each(val,function(index2,val2){
                        if (typeof subtotal[groupid] =='undefined'){
                            subtotal[groupid] = new Array();
                        }
                        if (typeof subtotal[groupid][index2] =='undefined'){
                            subtotal[groupid][index2] = 0;
                        }
    
                        value = Number(val2.replace('$',"").replace(',',"").replace('(',"-").replace(')',""));
    
                        subtotal[groupid][index2] += value;
                });
            });
    
    
    
                $('tbody').find('.group').each(function (i,v) {
                    var rowCount = $(this).nextUntil('.group').length;
                        var subtd = '';
                        for (var a=1;a<colnos;a++)
    
                        {   
                          var myClass = sumArray[a];
                          if (myClass == true) {
                            var dolClass = dolArray[a];
                              if (dolClass == true) {
                                subtd += '<td align=right style="background-color:wheat;">$'+parseFloat(subtotal[i][a]).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString()+'</td>';
                              }
                              else {
                                subtd += '<td align=right style="background-color:wheat;">'+parseFloat(subtotal[i][a]).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,").toString()+'</td>';
                              }
                          }
                          else {
                            subtd += '<td align=right style="background-color:wheat;"></td>';
                          }
                        }
                        $(this).append(subtd);
                    });
                }
    
This discussion has been closed.