demo D197
Running totals: JavaScript Data Grid Demo
The one derived value that depends on the display order
running: { of, kind: 'total' }
This grid shows a running total, the one derived value that depends on the display order, accumulating down the rows. It gives a cumulative figure that reads correctly in sequence, so a balance or a tally builds row by row.
Loading a live grid…
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.css">
<script src="https://cdn.latticegrid.dev/lattice-grid@1.103.2/lattice-grid.min.js"></script>
<div id="grid" style="height: 440px"></div>
<script>
const grid = LatticeGrid.createGrid(document.getElementById('grid'), {
rowKey: 'id',
columns: [
{ field: 'date', title: 'Date', type: 'dateString', layout: { pin: 'start', width: 150 } },
{ field: 'amount', title: 'Amount', type: 'number', format: { style: 'currency', currency: 'USD' }, total: 'sum' },
// running, not shadow: it reads in display order, so sorting the grid
// recomputes every value. Group headings and totals rows are skipped.
{ id: 'cum', title: 'Running', type: 'number', format: { style: 'currency', currency: 'USD' }, running: { of: 'amount', kind: 'total' } },
{ id: 'share', title: 'Cumulative %', type: 'number', format: { style: 'percent', decimals: 1 }, running: { of: 'amount', kind: 'percent' } },
],
state: { sort: [{ col: 'date', dir: 'asc' }] },
rows, // one row a day: date, amount
});
</script>