I've created a table with an aggregate and also with grouped rows. I've tried a few different ways to add an aggregate to group rows, but none worked. I was wondering what the best way to use the api to do this is.
A link to the debug page is here: http://debug.datatables.net/ikomex
The grouping code is in my drawCallback function:
drawCallback: function (settings) {
var api = this.api();
var depth = 1;
var rows = api.rows({ page: 'current' }).nodes();
//initialise last group names array
var lastNames = [];
if(api.data().length > 0)
depth = api.data()[0].AssetIdPath.match(/\./g).length;
for (var i = 0; i < depth; i++) {
lastNames.push("");
}
api.column(1, { page: 'current' }).data().each(function (group, i) {
var parentGroups = group.split("/");
for (var g = 0; g < depth; g++) {
if (lastNames[g] !== parentGroups[g]) {
$(rows).eq(i).before(
'<tr class="group"><td colspan="7">' + parentGroups[g] + '</td></tr>'
);
lastNames[g] = parentGroups[g];
}
}
});
}
This shows multi level groups based on the string in column 1, split by a forward slash. For example 1A / 1A.1
would show a parent group row for 1A
, one underneath it for 1A.1
, and any 1A.1
rows beneath that.
What I'd like to do is add a cost in here
'<tr class="group"><td colspan="7">' + parentGroups[g] + '</td></tr>'
I'm guessing I need to marry up the each loop with something like this (this is my totalling for the footer)
api
.column(6)
.data()
.reduce(function (a, b) {
return parseInt(a) + parseInt(b);
}, 0);
Except not using .column()
and maybe using .filter()
Anyone have any ideas? I've tried a few things, but don't think I grok the api fully yet. Sorry I can't get a live demo up.