demo D183
Statistical values in a column in htmx
A column's median, deviation and percentile, read into computed columns
value.compute
This grid reads a column's median, deviation and percentile into computed columns. It lets a per-row calculation reference a whole-column statistic, so a value can be compared against its column's own figures inline.
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="statistical-formulas-table" data-lattice-grid>
<thead>
<tr>
<th data-field="symbol">Symbol</th>
<th data-field="price">Price</th>
</tr>
</thead>
<tbody>
<tr data-id="INS-0000000"><td>ELMW.G0</td><td>462.0258</td></tr>
<tr data-id="INS-0000001"><td>RVNS.A1</td><td>241.8711</td></tr>
<tr data-id="INS-0000002"><td>NRTH.E2</td><td>98.9614</td></tr>
<!-- the rest of the rows, rendered by your server -->
</tbody>
</table>
<pre id="out" style="margin-top: 12px"></pre>
<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('statistical-formulas-table'), {
toolPanel: { side: 'left', panels: ['filters', 'columns'] },
columns: [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'price', title: 'Price', type: 'number' },
],
});
// Named reductions read straight from the grid over the rows that survive the
// filters, so every figure moves as the table is filtered.
const out = document.getElementById('out');
const report = () => {
out.textContent = 'median ' + grid.statistics.reduce('price', 'median')
+ ' · stddev ' + grid.statistics.reduce('price', 'stddev')
+ ' · p90 ' + grid.statistics.reduce('price', 'p90');
};
grid.on('filter:changed', report);
report();
</script>