Demo - Grand Totals on Top
by Joel Parks
HTML
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdn.wijmo.com/5.latest/styles/wijmo.min.css">
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.input.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.grid.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/controls/wijmo.chart.min.js"></script>
<script src="http://cdn.wijmo.com/5.latest/interop/angular/wijmo.angular.min.js"></script>
<!-- mark this as an Angular application and give it a controller -->
<div ng-app="app" ng-controller="appCtrl" class="container">
<h1>Custom Aggregates</h1>
<p>
This <b>FlexGrid</b> shows a <b>GroupRow</b> above the data
with "Sum" aggregates. This was done by adding a single
<b>GroupDescription</b> to the data source which groups
all rows under the same group:</p>
<wj-flex-grid
items-source="data"
group-header-format="Aggregates:"
frozen-rows="1"
format-item="formatItem(s,e)">
<wj-flex-grid-column header="Country" binding="country">
</wj-flex-grid-column>
<wj-flex-grid-column header="Sales" binding="sales" aggregate="Sum">
</wj-flex-grid-column>
<wj-flex-grid-column header="Expenses" binding="expenses" aggregate="Sum">
</wj-flex-grid-column>
<wj-flex-grid-column header="Margin" binding="margin" data-type="Number" format="p0" aggregate="Sum">
</wj-flex-grid-column>
<wj-flex-grid-column header="Profit" binding="profit" data-type="Number" format="n2" aggregate="Sum">
</wj-flex-grid-column>
</wj-flex-grid>
</div>
CSS
.wj-flexgrid {
max-height: 300px;
margin-top: 10px;
}
.negative {
color: red;
}
JavaScript
// define app, include Wijmo 5 directives
var app = angular.module('app', ['wj']);
// controller
app.controller('appCtrl', function($scope) {
// create some random data
var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
data = [];
for (var i = 0; i < 100; i++) {
data.push({
country: countries[i % countries.length],
downloads: Math.round(Math.random() * 20000),
sales: Math.random() * 10000,
expenses: Math.random() * 5000
});
}
// expose data as a CollectionView to get events
$scope.data = new wijmo.collections.CollectionView(data, {
groupDescriptions: [ ' ' ] // group on dummy property to create a single group
});
// use formatItem event to calculate custom aggregates
$scope.formatItem = function(s, e) {
if (e.panel == s.cells) {
var c = s.columns[e.col];
var item = s.rows[e.row].dataItem;
var negative = false;
if (c.binding == 'margin') {
var sales, expenses;
var row = s.rows[e.row];
if (row instanceof wijmo.grid.GroupRow) {
sales = item.getAggregate('Sum', 'sales');
expenses = item.getAggregate('Sum', 'expenses');
} else {
sales = item.sales;
expenses = item.expenses;
}
var margin = (sales / expenses) - 1;
e.cell.textContent = wijmo.Globalize.format(margin, c.format);
negative = margin < 0;
}
if (c.binding == 'profit') {
var profit = s.cells.getCellData(e.row, 'sales') -
s.cells.getCellData(e.row, 'expenses');
e.cell.textContent = wijmo.Globalize.format(profit, c.format);
negative = profit < 0;
}
wijmo.toggleClass(e.cell, 'negative', negative);
}
}
});