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>
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.css">
<div id="app"></div>
<script type="module">
import React from 'https://esm.sh/react@18';
import { createRoot } from 'https://esm.sh/react-dom@18/client';
import * as Lattice from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/modules/react.esm.min.js';
const LatticeGrid = createLatticeGrid({ React, createGrid: Lattice.createGrid });
const { createUnitType, registerUnitSystem, defineUnit } = Lattice;
// 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,
};
function App() {
const gridRef = React.useRef(null);
return (
<>
<LatticeGrid ref={gridRef} {...config} style={{ height: '330px' }} />
</>
);
}
createRoot(document.getElementById('app')).render(<App />);
</script>
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.css">
<style>
#app > div:first-child { height: 330px; }
</style>
<div id="app"></div>
<script type="module">
import * as Vue from 'https://esm.sh/vue@3/dist/vue.esm-browser.prod.js';
import * as Lattice from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.esm.min.js';
import createLatticeGrid from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/modules/vue.esm.min.js';
const LatticeGrid = createLatticeGrid({ vue: Vue, createGrid: Lattice.createGrid });
const { createUnitType, registerUnitSystem, defineUnit } = Lattice;
// 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,
};
Vue.createApp({
components: { LatticeGrid },
data() {
// markRaw: the grid's data and source objects are not for Vue to wrap.
return { config: Vue.markRaw(config) };
},
template: '<lattice-grid ref="grid" v-bind="config" />',
}).mount('#app');
</script>
<script>
import * as Lattice from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.esm.min.js';
import createLatticeAction from 'https://cdn.latticegrid.dev/lattice-grid@1.133.1/modules/svelte.esm.min.js';
const lattice = createLatticeAction({ createGrid: Lattice.createGrid });
const { createUnitType, registerUnitSystem, defineUnit } = Lattice;
// 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 },
];
// onGrid hands over the live grid the moment it is created.
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,
};
</script>
<svelte:head>
<link rel="stylesheet" href="https://cdn.latticegrid.dev/lattice-grid@1.133.1/lattice-grid.min.css">
</svelte:head>
<div use:lattice={config} style="height: 330px"></div>
import { Component, ViewChild } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import * as Lattice from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { LatticeGridComponent, provideLattice } from '@toclocoinc/lattice-grid/angular';
const { createUnitType, registerUnitSystem, defineUnit } = Lattice;
// 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 },
];
@Component({
selector: 'app-root',
standalone: true,
imports: [LatticeGridComponent],
template:
'<lattice-grid [config]="grid" style="display:block;height:330px"></lattice-grid>',
})
export class AppComponent {
@ViewChild(LatticeGridComponent) gridRef!: LatticeGridComponent;
grid = {
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,
};
}
bootstrapApplication(AppComponent, {
providers: [provideLattice({ createGrid: Lattice.createGrid })],
});
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.macFormatchooses 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,21becomes10,20-30).typeOptions.vlanSingle: truelimits the column to a single ID. -
cidr:typeOptions.cidrNetwork: truestores a block as its network, so10.1.2.3/24becomes10.1.2.0/24. -
ipv4andcidr: an edit saves the address as text, andtypeOptions.storeNumber: truekeeps 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.