Lattice Grid Buy a licence

demo D208

Configuring a type in htmx

One field, five presentations; the stored value never changes

createUnitType({ display, decimals, … })

This grid shows one stored field presented five different ways, with the underlying value never changing. It shows that display is a setting, not a rewrite, so you can change decimals, symbols or format without touching the data behind the column.

Building…
Loading a live grid…

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="type-configuration-table" data-lattice-grid>
  <thead>
    <tr>
      <th data-field="run">Run</th>
      <th data-field="span">Stored (m)</th>
    </tr>
  </thead>
  <tbody>
    <tr data-id="1"><td>Run 001</td><td>1645.736</td></tr>
    <tr data-id="2"><td>Run 002</td><td>874.552</td></tr>
    <tr data-id="3"><td>Run 003</td><td>1835.459</td></tr>
    <!-- the rest of the rows, rendered by your server -->
  </tbody>
</table>

<script type="module">
  import { hydrateTable, createUnitType } from 'https://cdn.latticegrid.dev/lattice-grid@1.107.0/modules/htmx.esm.min.js';

  // The same field stored in metres, shown four ways. The stored value never
  // changes; only the display does.
  const dataTypes = {
    spanAuto: createUnitType({ system: 'length', unit: 'm', display: 'auto' }),
    spanMm:   createUnitType({ system: 'length', unit: 'm', display: 'mm' }),
    spanKm:   createUnitType({ system: 'length', unit: 'm', display: 'km', decimals: 3 }),
    spanSig:  createUnitType({ system: 'length', unit: 'm', display: 'auto', significantFigures: 3 }),
  };

  // 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('type-configuration-table'), {
    dataTypes,
    columns: [
      { field: 'run', title: 'Run', layout: { pin: 'start', width: 120 } },
      { field: 'span', id: 'stored', title: 'Stored (m)', type: 'number' },
      { field: 'span', id: 'auto', title: "display: 'auto'", type: 'spanAuto' },
      { field: 'span', id: 'mm', title: "display: 'mm'", type: 'spanMm' },
      { field: 'span', id: 'km', title: "display: 'km'", type: 'spanKm' },
      { field: 'span', id: 'sig', title: '3 sig figs', type: 'spanSig' },
    ],
  });
</script>