Group Sub Totals

Group Sub Totals

rocky2016rocky2016 Posts: 11Questions: 6Answers: 0

With this reference, I can find the grouping but I would have to display the sub totals of each group and a final grand total.

https://datatables.net/examples/advanced_init/row_grouping.html

I couldnt find any profound solution

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,893Questions: 1Answers: 10,531 Site admin
    Answer ✓

    I don't think anyone has posted a solution for exactly what you are looking for. I hope to write a proper plug-in for DataTables that will do that sometime, but at the moment you would need to modify the example code to perform the sum in the group.

    Allan

  • rocky2016rocky2016 Posts: 11Questions: 6Answers: 0

    Below code I wrote worked for me for sub totals..

                      "drawCallback": function(settings) {
                        var api = this.api();
                        var rows = api.rows({ page: 'current' }).nodes();
                        var last = null;
                        var cols = api.row(0).data().length;
                        var totale = new Array();
                        totale['Totale'] = new Array();
                        var groupid = -1;
                        var subtotale = new Array();
                        api.column(0, { page: 'current' }).data().each(function(group, i) {
                            console.log('group', group);
                            console.log('index', i);
                            //var groupEnd=i;     
                            if (last !== group) {
                                groupid++;
                                console.log('GroupID after', groupid);
                                $(rows).eq(i).before(
                                    '<tr class="group"><td> Total ' + group + '</td></tr>'
                                    );
                                last = group;
                            }
                        var val = api.row(api.row($(rows).eq(i)).index()).data();      //current order index
                            $.each(val, function(index2, val2) {
                                if (typeof subtotale[groupid] == 'undefined') {
                                    subtotale[groupid] = new Array();
                                }
                                if (typeof subtotale[groupid][index2] == 'undefined') {
                                    subtotale[groupid][index2] = 0;
                                }
    
                                var valore = Number(val2.replace('(', '-').replace('$', "").replace(',', "").replace(',', "").replace(')', ""));
                                console.log('valore', valore);
                                //subtotale[groupid][index2] += valore;
                                subtotale[groupid][index2] = +parseFloat(subtotale[groupid][index2] + valore).toFixed(2);
                                console.log('sub total', subtotale[groupid][index2]);
    
                            });
                        });
                        $('tbody').find('.group').each(function(i, v) {
                            var rowCount = $(this).nextUntil('.group').length;
                            $(this).find('td:first').append($('<span />', { 'class': 'rowCount-grid' }).append($('<b />', { 'text': ' (' + rowCount + ')' })));
                            var subtd = '';
                            for (var a = 2; a < cols; a++) {
                                subtd += '<td align="right">' + $.fn.dataTable.render.number(',', '.', 2, '$').display(subtotale[i][a]) + '</td>';
                            }
                            $(this).append(subtd);
                        });
                    }
                });
    
This discussion has been closed.