Summing column with subtotals
Summing column with subtotals
I am trying to sum a column of numbers that have subtotals without including the subtotals in the total. I have added the class of "skipit" to the subtotals and am looking for an approach that will selectively add the column excluding those having the skipit class. Near all the examples I can find are using the .reduce method and that would seem to eliminate the possibility of class making any difference.
So I have pieced together the following from various posts but it seems I am still some distance from arriving at a workable solution.
Summing column 4:
var intVal = function (i) {
return typeof i === 'string' ?
i.replace(/[\$,]/g, '') * 1 :
typeof i === 'number' ?
i : 0;
};
var api = this.api(),
totala = 0;
api.rows(":not('.skipit')").every(function () {
totala +=intVal(this.data()[4]);
});
$(api.column(4).header()).html(totala);
Any help would be appreciated.
Thanks,
Jim
Answers
Hi Jim,
Something like this here will do the trick. I've set "skipit" on all cities other than "New York" - there are only two so it keeps the maths simple The script sums up those without "skipit" and outputs to the console.
Hope that helps,
Cheers,
Colin
Brilliant, Colin.
Many thanks for your quick and thorough response. This was helpful far beyond my initial question (which it dealt with handily). You have now cost me a bunch of time as I rewrite code in other projects.
Again, many thanks,
Jim