Lattice Grid Buy a licence

demo D217

A type of your own in htmx

A part number, AB-1234-X, in five functions: matches, parse, format, compare

dataTypes: { partNumber: { base, matches, parse, format, compare } }

This grid teaches the grid a part-number format of your own through five small functions: match, parse, format and compare. It shows how any domain value can become a first-class type that sorts, filters and edits correctly, not just a string that happens to look right.

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="custom-data-type-table" data-lattice-grid>
  <thead>
    <tr>
      <th data-field="part">Part number</th>
      <th data-field="description">Description</th>
      <th data-field="qty">On hand</th>
      <th data-field="price">Unit price</th>
    </tr>
  </thead>
  <tbody>
    <tr data-id="1"><td>AB-1000-A</td><td>Bracket, mounting, left</td><td>240</td><td>3.75</td></tr>
    <tr data-id="2"><td>ab9b</td><td>Bracket, mounting, right</td><td>240</td><td>3.75</td></tr>
    <tr data-id="3"><td>CD 1234 X</td><td>Housing, upper</td><td>60</td><td>41.2</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';

  // A part number, AB-1234-X: a family, a serial and a revision. A type is a
  // small set of functions that claim a value, parse it, format it and compare
  // it; this defines them and registers the type under one name.
  const PART = /^\s*([A-Za-z]{2})[-\s]?(\d{1,4})[-\s]?([A-Za-z])\s*$/;
  const read = (v) => {
    const m = PART.exec(String(v ?? ''));
    return m ? { family: m[1].toUpperCase(), serial: +m[2], rev: m[3].toUpperCase() } : null;
  };
  const canonical = (v) => {
    const p = read(v);
    return p ? `${p.family}-${String(p.serial).padStart(4, '0')}-${p.rev}` : null;
  };

  const partNumber = {
    base: 'text',
    matches: (v) => read(v) != null,                       // claim the value when inferring
    parse: ({ text }) => canonical(text) ?? text,          // ab12x  ->  AB-0012-X
    format: ({ value }) => canonical(value) ?? String(value ?? ''),
    compare: (a, b) => {                                   // sort on the serial as a number
      const x = read(a), y = read(b);
      if (!x || !y) return x ? -1 : y ? 1 : 0;
      return x.family.localeCompare(y.family) || x.serial - y.serial || x.rev.localeCompare(y.rev);
    },
  };

  // 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('custom-data-type-table'), {
    dataTypes: { partNumber },                             // registered in one line
    columns: [
      { field: 'part', title: 'Part number', type: 'partNumber', editor: 'text', layout: { width: 160, pin: 'start' } },
      { field: 'description', title: 'Description', layout: { width: 240 } },
      { field: 'qty', title: 'On hand', type: 'number', total: 'sum' },
      { field: 'price', title: 'Unit price', type: 'number',
        format: { style: 'currency', currency: 'GBP' }, total: 'mean' },
    ],
    state: { sort: [{ col: 'part', dir: 'asc' }] },
  });
</script>