Lattice Grid

developer guide

Data Types and Units: JavaScript Data Grid Guide

Ninety-six technical types on top of the seven built-ins, each with its own parser, comparator, editor, filter and Excel mapping.

Try it: Unit columns that store one number and show it well

Each column stores a single base number and only the display changes. Sort Length or Weight and the order is by quantity, not by text. The Yarn count column uses a unit family registered in the first lines of the code. Click a cell to edit it and type another unit, such as 2.5 km or 800 g.

Loading a live grid…

<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.css">
<script src="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.js"></script>

<div id="grid" style="height: 330px"></div>

<script>
  const { createUnitType, registerUnitSystem, defineUnit } = LatticeGrid;

  // A family of your own: factor is how many BASE units one of these is.
  registerUnitSystem('yarn', [
    defineUnit('tex', 1, ['tx']),
    defineUnit('ktex', 1e3, [], { prefix: 'k' }),
    defineUnit('den', 1 / 9, ['denier']),
  ]);

  const rows = [
    { roll: 'R-2041', fabric: 'Cotton twill', length: 1250, width: 1500, weight: 410, yarn: 29.5 },
    { roll: 'R-2042', fabric: 'Linen plain', length: 880, width: 1400, weight: 265, yarn: 48 },
    { roll: 'R-2043', fabric: 'Wool flannel', length: 420, width: 1500, weight: 310, yarn: 95 },
    { roll: 'R-2044', fabric: 'Denim 12oz', length: 1800, width: 1520, weight: 880, yarn: 59 },
    { roll: 'R-2045', fabric: 'Silk habotai', length: 96, width: 1100, weight: 14, yarn: 5.5 },
    { roll: 'R-2046', fabric: 'Canvas heavy', length: 640, width: 1800, weight: 760, yarn: 240 },
    { roll: 'R-2047', fabric: 'Viscose crepe', length: 2.4e3, width: 1400, weight: 372, yarn: 18 },
  ];

  const config = {
    rowKey: 'roll',
    dataTypes: {
      // Stored in metres; the display walks the ladder: mm, cm, m, km.
      rollLength: createUnitType({ system: 'length', unit: 'm', display: 'auto', maxDecimals: 2 }),
      // Stored in millimetres, always shown in centimetres.
      rollWidth: createUnitType({ system: 'length', unit: 'mm', display: 'cm', decimals: 0 }),
      // Stored in kilograms; a light roll reads in grams.
      rollWeight: createUnitType({ system: 'mass', unit: 'kg', display: 'auto', maxDecimals: 1 }),
      // The family registered above.
      yarnCount: createUnitType({ system: 'yarn', unit: 'tex', display: 'auto', maxDecimals: 1 }),
    },
    columns: [
      { field: 'roll', title: 'Roll', layout: { pin: 'start', width: 100 } },
      { field: 'fabric', title: 'Fabric', layout: { width: 150 } },
      { field: 'length', title: 'Length', type: 'rollLength', edit: { enabled: true }, layout: { width: 120 } },
      { field: 'width', title: 'Width', type: 'rollWidth', layout: { width: 110 } },
      { field: 'weight', title: 'Weight', type: 'rollWeight', edit: { enabled: true }, layout: { width: 120 } },
      { field: 'yarn', title: 'Yarn count', type: 'yarnCount', layout: { width: 130 } },
    ],
    rows,
  };

  const grid = LatticeGrid.createGrid(document.getElementById('grid'), config);
</script>

A type is a whole behaviour

A column's type decides how it stores, sorts, parses, filters, aligns, renders, copies and exports. Beyond the seven built-ins (text, number, date, boolean, lookup, object, image), the grid ships ninety-six technical types, grouped by domain: network (ipv4, ipv6, cidr, mac, vlan), time (duration, datetime), radix (hex, binary), data (bytes, bitrate), and unit families across mechanical, fluid and thermal, energy, electrical, chemistry and radiation.

See them side by side in the type catalogue demo.

Network types

  • mac: a MAC address in any common spelling (colons, hyphens, Cisco dots or bare hex), stored one way and sorted by value. typeOptions.macFormat chooses how it is shown: 'colon', 'colon-upper', 'cisco' or 'hyphen'.
  • vlan: one VLAN ID or a list of IDs and ranges from 1 to 4094, stored tidied and sorted (30-20, 10,25,21 becomes 10,20-30). typeOptions.vlanSingle: true limits the column to a single ID.
  • cidr: typeOptions.cidrNetwork: true stores a block as its network, so 10.1.2.3/24 becomes 10.1.2.0/24.
  • ipv4 and cidr: an edit saves the address as text, and typeOptions.storeNumber: true keeps saving the number instead.

The stored value never changes

A unit type stores one base number and only the display changes, which is the whole design: sorting, filtering, grouping, totals and the pivot all read the number, so 250mm sorts below 1.5 cm correctly rather than 1 sorting before 9. The editor accepts any unit on the ladder, 1.5 GB or a bare byte count, and resolves it to the base on entry.

Configuring a type

Every unit type is built with createUnitType and takes the same options.

dataTypes: {
  span: createUnitType({
    system: 'length',
    unit: 'm',            // what the column STORES
    display: 'auto',      // walk the ladder, or a fixed symbol like 'mm'
    placement: 'suffix',  // or 'prefix', for currencies
    decimals: 2,          // or minDecimals / maxDecimals / significantFigures
  }),
},
columns: [{ field: 'span', type: 'span' }],

A family of your own

Three things define a family and all three are yours: the symbols, where the symbol sits relative to the number, and how the rungs relate. The factor values are the ladder, sorted at load, so there is no separate ordering to declare and no way to get it wrong. Exactly one unit must have a factor of 1, the base.

import { registerUnitSystem, defineUnit, createUnitType } from '@toclocoinc/lattice-grid';

// factor is how many BASE units one of these is. Exactly one must be 1.
registerUnitSystem('yarn', [
  defineUnit('tex',  1,     ['tx']),
  defineUnit('ktex', 1e3,   [], { prefix: 'k' }),
  defineUnit('den',  1 / 9, ['denier']),
]);

createGrid(el, {
  dataTypes: { linearDensity: createUnitType({ system: 'yarn', unit: 'tex', display: 'auto' }) },
  columns: [{ field: 'count', type: 'linearDensity' }],
});

registerUnitSystem is global and throws on a duplicate name, so register once at startup rather than inside a component that may mount twice. See the custom units demo.

The type story runs into statistics, where a column that declares a spec drives process capability, and the rest of the developer guide.