demo D184
Shadow columns: what changed in htmx
Delta, percent change and a move count against the value at page load
shadow: { of, kind: 'delta' }
This grid adds shadow columns showing each value's delta, percent change and move count against its value at page load. It lets a reader see what has moved since they arrived, as real sortable columns beside the live figures.
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="shadow-columns-change-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>
<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('shadow-columns-change-table'), {
highlightOnChange: { duration: 500 },
columns: [
{ field: 'symbol', title: 'Symbol', layout: { pin: 'start', width: 120 } },
{ field: 'price', title: 'Price', type: 'number' },
// A shadow column is declared against another and maintained by the grid.
{ id: 'delta', title: 'Change', shadow: { of: 'price', kind: 'delta' }, type: 'number', format: { signed: true } },
{ id: 'pct', title: 'Change %', shadow: { of: 'price', kind: 'deltaPercent' }, type: 'number', format: { style: 'percent', signed: true } },
{ id: 'moves', title: 'Moves', shadow: { of: 'price', kind: 'updates' }, type: 'number' },
{ id: 'streak', title: 'Streak', shadow: { of: 'price', kind: 'streak' }, type: 'number', format: { signed: true } },
],
});
</script>