Trying to sum multiple columns

Trying to sum multiple columns

beanie1337beanie1337 Posts: 3Questions: 2Answers: 0

Hello.

I've a column for each week of the year containing different numbers. So when each column renders I do a sum for that column in the footer. But I also would like to do a sum for that column and 3 months ahead (this + 11 columns ahead) for each column.

   footerCallback: function () {
            var api = this.api();
            api.columns('.sum', {
                page: 'current'
            }).every(function (index) {
                var sum = this.data().sum();

                // How should I do this instead of referencing the table again? Or is this OK?
                // I cannot reach the table variable created when creating the DataTable in document ready.
                var table2 = $('#assignmentReport').DataTable();
                for (var i = index; i <= index + 11; i++) {
                        var s = table2.column(i).data().sum();
                        console.log(s)
                }
            });
        }

The problem with this solution is that it continues for 11 weeks the last week 52 also, so it throws an error:

Cannot read property 'sDefaultContent' of undefined

Could someone guide me in the right direction?

Thanks in advance.

Answers

  • kthorngrenkthorngren Posts: 20,145Questions: 26Answers: 4,736

    How should I do this instead of referencing the table again?

    Use the api variable from var api = this.api();.

    The problem with this solution is that it continues for 11 weeks the last week 52 also, so it throws an error:

    Not sure I totally understand but it sounds like you are describing that table2.column(i) is trying to access columns that aren't in the table. If so then just an if statement to set the value of 11 in for (var i = index; i <= index + 11; i++). Change the 11 to be a variable.

    Kevin

This discussion has been closed.