demo D179
Statistics in the totals row in htmx
Median, percentiles, deviation and spread, each over the filtered rows
total: 'median' · 'p95' · 'stddev'
This grid puts statistics in the totals row, median, percentiles, standard deviation and spread, each computed over the filtered rows. It gives a footer that summarises the shape of a column, not just its sum, and follows the filter as it changes.
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.108.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="statistical-totals-table" data-lattice-grid>
<thead>
<tr>
<th data-field="reference">Reference</th>
<th data-field="desk">Desk</th>
<th data-field="price">Price</th>
<th data-field="bid">Bid</th>
<th data-field="volume">Volume</th>
<th data-field="notional">Notional</th>
</tr>
</thead>
<tbody>
<tr data-id="1"><td>TRD-4101</td><td>London</td><td>101.42</td><td>101.38</td><td>1250</td><td>126775</td></tr>
<tr data-id="2"><td>TRD-4102</td><td>New York</td><td>98.76</td><td>98.7</td><td>3400</td><td>335784</td></tr>
<tr data-id="3"><td>TRD-4103</td><td>Tokyo</td><td>104.05</td><td>103.99</td><td>900</td><td>93645</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.108.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('statistical-totals-table'), {
columnDefaults: { filter: true },
columns: [
{ field: 'reference', title: 'Reference', layout: { pin: 'start', width: 150 } },
{ field: 'desk', title: 'Desk', filter: { type: 'set' } },
// Each column names its own reduction: median, 95th percentile, deviation.
{ field: 'price', title: 'Price', type: 'number', total: 'median' },
{ field: 'bid', title: 'Bid', type: 'number', total: 'p95' },
{ field: 'volume', title: 'Volume', type: 'number', total: 'iqr' },
{ field: 'notional', title: 'Notional', type: 'number', total: 'sum' },
],
});
</script>