Using fixedcolumns and footerCallback does not sum all of the columns.

Using fixedcolumns and footerCallback does not sum all of the columns.

Keith_HKeith_H Posts: 54Questions: 19Answers: 1
edited April 2018 in Free community support

My table is defined as

        if (pTable == 'tblTaskWbsBudgetSumm') {
            $('#tblTaskWbsBudgetSumm').DataTable({
                  "autoWidth":false
                , "fixedColumns": {leftColumns: 4 }
                , "footer": true
                , "info":false
                , "language": {"emptyTable":"No Data Found"}
                , "JQueryUI":true
                , "Order":[0,'asc']
                , "ordering":false
                , "paging":false
                , "scrollY":"600px"
                , "scrollX":"1800px"
                , "scrollCollapse":true
                , "columnDefs": [
                            { className: "LeftNoWrap", "targets": [ 0,1] }
                    ]
                , "footerCallback": function ( row, data, start, end, display ) {
                                var api = this.api(), data,total;

                                api.columns('.sum0', {}).every(function() {
                                    var locSum = this
                                        .data()
                                        .reduce(function(a, b) {return fncSumDataTableColumn(a,b);}, 0);
                                    $(this.footer()).html(fncFormatNumber(locSum,0,"Y","Y"));
                                });
                                api.columns('.sum2', {}).every(function() {
                                    var locSum = this
                                        .data()
                                        .reduce(function(a, b) {return fncSumDataTableColumn(a,b);}, 0);
                                    $(this.footer()).html(fncFormatNumber(locSum,2,"Y","Y"));
                                });
                        }
            });
        }

Columns 1 & 2 are text.
Columns 3 & 4 are the totals (hours and values) for the remaining n columns in each row. Columns after column 4 all get summed fine.

The problem I am getting is:

If I have "fixedColumns": {leftColumns: 2 }, then columns 3 & 4 get summed into the footer.

If I have "fixedColumns": {leftColumns: 4 }, then columns 3 & 4 don't get summed into the footer.

For some reason, the footerCallback does not get fired on the fixed columns.

I want to fix the total columns, so that when the user scrolls horizontally through the columns, they still see the totals.

Answers

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    I built this example for another thread and it works with FixedColumns:
    http://live.datatables.net/terosali/1/edit

    Can you build a test case showing the issue?
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • Keith_HKeith_H Posts: 54Questions: 19Answers: 1

    I've added http://live.datatables.net/tiduvico/1/edit (I think anyway ;) ).

    I've not added the fixedcolumn libraries so it's not scrolling horizontally.

    Hope that helps.

    Thanks.

  • Keith_HKeith_H Posts: 54Questions: 19Answers: 1
    edited April 2018

    I've added the other libraries to the above example and it totals all columns correctly.

    However, my data is being added from json.

            $.getJSON(glbUrl, function(data) {
    
                for (var i = 0; i < data.length; i++) {
                    locRow=[];
                    locRow.push('<a id="aTaskWbsBudgetEdit'+trim(data[i].WSWBS)+'" href="#" >'+trim(data[i].WSWBS)+'</a>');
                    locRow.push(trim(data[i].WSWBSDESC));
                    locRow.push(fncFormatNumber(data[i].WSTOTALHOURS,0,"Y","Y"));
                    locRow.push(fncFormatNumber(data[i].WSTOTALCOSTS,0,"Y","Y"));
                    for (var j = 0; j < glbTaskPeriodArray.length; j++) {
                        var locPd = glbTaskPeriodArray[j];
                        locRow.push(fncFormatNumber(data[i][locPd+'_HOURS'],0,"Y","Y"));
                        locRow.push(fncFormatNumber(data[i][locPd+'_COSTS'],0,"Y","Y"));
                    }
    
                    $('#tblTaskWbsBudgetSumm').DataTable().row.add(locRow);
                }
                $('#tblTaskWbsBudgetSumm').DataTable().columns.adjust().draw();
            });
    
    
  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    Hmm, using JSON data shouldn't make a difference. Maybe you can get a few rows example from your array and use it for the data in the example.

    Not sure if it would help performance wise but you could build an array of your rows in the for loop then just use the below after your loop to add all the rows at oince:
    $('#tblTaskWbsBudgetSumm').DataTable().rows.add(locRow);

    Not that it will help the footerCallback :smile:

    BTW, I added the FixedColumn library to your example:
    http://live.datatables.net/tiduvico/3/edit

    Kevin

  • Keith_HKeith_H Posts: 54Questions: 19Answers: 1
    edited April 2018

    Hi Kevin.

    I don't know if this helps, but when I examine the table elements, the footer in the fixed table doesn't have the total values, but the scrolling table does. So the totals are being created, but only in the left hand table, so they aren't visible.

This discussion has been closed.