demo D197
Running totals in htmx
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 htmx version. A server-rendered table is upgraded in place, so the page is readable before any script runs and gains the full grid when the module loads, which suits a progressively enhanced app.
The configuration
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.107.0/lattice-grid.min.css">
<!-- A real, server-rendered table: readable with JavaScript off. data-field on
each header names the column that cell becomes, and data-id on each row is
the key the grid carries it under. -->
<table id="running-totals-table" data-lattice-grid>
<thead>
<tr>
<th data-field="date">Date</th>
<th data-field="amount">Amount</th>
</tr>
</thead>
<tbody>
<tr data-id="1"><td>2026-01-01</td><td>520</td></tr>
<tr data-id="2"><td>2026-01-02</td><td>833.33</td></tr>
<tr data-id="3"><td>2026-01-03</td><td>909.54</td></tr>
<!-- the rest of the rows, rendered by your server -->
</tbody>
</table>
<script type="module">
import { hydrateTable } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/htmx.esm.min.js';
// The markup carries the rows; a server-rendered table's column types are
// declared in the config hydrateTable takes. The grid replaces the table
// where it stood.
const grid = hydrateTable(document.getElementById('running-totals-table'), {
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' } },
],
state: { sort: [{ col: 'date', dir: 'asc' }] },
});
</script>