row grouping with ajax data

row grouping with ajax data

sam99sam99 Posts: 1Questions: 1Answers: 0

Hi all,

i ma new in using DataTables and have this kind of problem. I am using datatables with ajax data source and i want to group similar rows and then do some summary calculations on each group row. But here is the problem, that i cant use row grouping plugin due to javascript error. But strange thing is that if i use data in html everything goes well, but now i am getting data from ajax source and it doesn't work. Please could somebody give me a hint how can i achieve this.

                                 var table = $('#productTable').DataTable(
                                        {
                                            bAutoWidth : false,
                                            ordering : false,
                                            bPaginate : true,
                                            bFilter : true,
                                            bLengthChange : true,
                                            searchHighlight : true,
                                            iDisplayLength : 25,
                                            bInfo : true,
                                            serverSide : "true",
                                            ajax : {
                                                url : rootPath + "/testserv/getProduct/"+range,
                                                data : function(data) {

                                                },
                                                error : function(){

                                                }

                                            },
                                            columns:[
                                                {
                                                    name: "dtStart",
                                                    data:"dtStart",
                                                    sClass : "center",
                                                },{
                                                    name:"vat",
                                                    data:"vat",
                                                    sClass : "center"
                                                },{
                                                    name:"orderDistinctCount",
                                                    data:"orderDistinctCount",
                                                    sClass : "center"
                                                },{
                                                    name:"totalBilledWithoutVat",
                                                    data:"totalBilledWithoutVat",
                                                    sClass : "right"
                                                },{
                                                    name: "totalBilledWithVat",
                                                    data: "totalBilledWithVat",
                                                    sClass: "right"
                                                },{
                                                    name:"vatTotal",
                                                    data:"vatTotal",
                                                    sClass: "right"
                                                },{
                                                    name: "avgBilledWithVAT",
                                                    data:"avgBilledWithVAT",
                                                    sClass: "right"
                                                },{
                                                    name:"profitWithoutVAT",
                                                    data:"profitWithoutVAT",
                                                    sClass: "right"
                                                },{
                                                    name:"totalDiscountProvidedWithVat",
                                                    data:"totalDiscountProvidedWithVat",
                                                    sClass : "right"
                                                },{
                                                    name:"branchname",
                                                    data:"branchname",
                                                    sClass : "right"
                                                }

                                            ],
                                            columnDefs : [
                                                {
                                                aTargets : [ 1,3,4,5,6 ],
                                                mRender : function(data, type, full) {
                                                    var regex = new RegExp(' ', 'g');
                                                    return data.replace(regex, ' ');
                                                }
                                            },
                                                {
                                                aTargets : [0],
                                                width : "10%",
                                                mRender: function(data, type, full) {
                                                    return getDateGranulLink(data,granul,full.id);
                                                    }
                                            } ],
                                            "initComplete" : function(settings) {
                                                var api = this.api();
                                                if (api.data().length > 0) {
                                                    var rows = api.rows({page: 'current'}).nodes();
                                                    var fields = api.row(0).data().length;
                                                    var sum = new Array();
                                                    var subSum = new Array();
                                                    var groupid = -1;
                                                    var last = null;

                                                    api.column(0, {page: 'current'}).data().each(function (group, i) {
                                                        if (last != group) {
                                                            groupid++;
                                                            $(rows).eq( i ).before(
                                                                    '<tr class="group "><td>'+group+'</td></tr>'
                                                            );
                                                            last = group;

                                                        }

                                                        var val = api.row(api.row($(rows).eq(i)).index()).data();
                                                        $.each(val, function (index2, val2) {
                                                            if (typeof subSum[groupid] == 'undefined') {
                                                                subSum[groupid] = new Array();
                                                            }
                                                            if (typeof subSum[groupid][index2] == 'undefined') {
                                                                subSum[groupid][index2] = 0.00;
                                                            }

                                                            if (val2 != "-" && isNaN(val2) ) {
                                                                subSum[groupid][index2] += parseFloat(val2.replace(" $", "").replace(',', ".").replace(/&nbsp;/g, ""));
                                                            }

                                                        });

                                                    });

                                                    $('tbody').find('[id*=group]').each(function (i, v) {
                                                        var rowCount = $(this).nextUntil('[id*=group]').length;
                                                        $(this).find('td:first').attr('colspan', 2);
                                                        var subtd = '';
                                                        for (var a = 3; a < fields; a++) {

                                                            subSum[i][a] = getMoneyFormatted(subSum[i][a])


                                                            if (a == 9) {
                                                                subSum[i][a] = '&nbsp;';
                                                            }
                                                            subtd += '<td class="group right text-sum">' + subSum[i][a] + '</td>';
                                                        }
                                                        $(this).append(subtd);

                                                    })

                                                }
                                            },
                                        });


                    $('#productTable').dataTable().rowGrouping({

                    });
This discussion has been closed.