demo D197
Running totals in React
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.
This is the React version. The grid mounts through the createLatticeGrid adapter, which takes its configuration as ordinary props and hands back the live grid through a ref. The grid below is the same one every other tab runs.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<div id="grid"></div>
<script type="module">
import React from 'https://esm.sh/react@18';
import { createRoot } from 'https://esm.sh/react-dom@18/client';
import { createGrid } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/react.esm.min.js';
const LatticeGrid = createLatticeGrid({ React, createGrid });
const columns = [
{ field: 'date', title: 'Date', type: 'date', layout: { pin: 'start', width: 150 } },
{ field: 'amount', title: 'Amount', type: 'number', total: 'sum' },
// A running total depends on the display order, so it is running, not shadow.
{ id: 'cum', title: 'Running', running: { of: 'amount', kind: 'total' }, type: 'number' },
{ id: 'share', title: 'Cumulative %', running: { of: 'amount', kind: 'percent' }, type: 'number', format: { style: 'percent' } },
];
const rows = [/* dated amounts */];
function App() {
return (
<LatticeGrid
rowKey="id"
columns={columns}
state={{ sort: [{ col: 'date', dir: 'asc' }] }}
rows={rows}
style={{ height: '460px' }}
/>
);
}
createRoot(document.getElementById('grid')).render(<App />);
</script>