My data has more fields than my page has columns. I need to sum fields into the columns.
The JSON I have:
{
'foo1' : 10,
'foo2' : 11,
'foo3' : 5,
'bar1' : 23,
'bar2' : 2,
'baz1' : 53,
'baz2' : 43
},{
'foo1' : 11,
'foo2' : 41,
'foo3' : 31,
etc.
}
The GRID I want:
Foos Bars Baz's
---- ---- -----
26 23 96
83 etc.
So I want to be able to add multiple items before rendering the field, something like this:
columnDefs: [{
field: 'foo1 + foo2 + foo3',
displayName: 'Foos'
},{
field: 'bar1 + bar2',
displayName: 'Bars'
},{
field: 'baz1 + baz2',
displayName: 'Baz's'
}]
or maybe something like
field : 'sumFields([foo1,foo2,foo3])'
...
$scope.sumFields = function(fieldlist){
return fieldlist.reduce(function(pv, cv) { return pv + cv; }, 0));
};
Of course, none of this pseudo-code works.
And I'd like to avoid writing a custom CellTemplate for every column.
I suppose, worst-case scenario, I could massage the data before rendering... So,
{
'foo1' : 10,
'foo2' : 11,
'foo3' : 5,
'bar1' : 23,
'bar2' : 2,
'baz1' : 53,
'baz2' : 43,
'foos': 26,
'bars': 23,
'bazs': 96
},{
'foo1' : 11,
'foo2' : 41,
'foo3' : 31,
etc.
}
[ EDIT ]I've added a sample plunker that contains my unmodified grid, and a depiction of what I want to have:
http://plnkr.co/edit/x5abGuyvunBDes6h60eg?p=preview
I don't want to see cats or dogs or other animals, I want to see a sum of certain types. There are more of these columns than I show, so I'm looking for a more general solution, something like
numAnimalsByType([arrayOfAnimals])
so I can do this:
field: numAnimalsByType([dog,cat])
field: numAnimalsByType([fish,snail])