Lattice Grid Buy a licence

demo D217

A type of your own

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

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

Building…
Loading a live grid…

The configuration

import * as ng from '@angular/core';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import createLatticeGrid from '@toclocoinc/lattice-grid/modules/angular';

const { LatticeGridComponent } = createLatticeGrid({ ng, createGrid });

// A part number, AB-1234-X: a family, a serial and a revision. A type is a
// few functions, registered 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);
  },
};

const 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' },
];

const rows = [/* e.g. [{ id: 1, part: 'AB-1000-A', description: 'Bracket', qty: 240, price: 3.75 }, ...] */];



@Component({
  selector: 'app-root',
  standalone: true,
  imports: [LatticeGridComponent],
  template: '<lattice-grid [config]="grid" style="display:block;height:480px"></lattice-grid>',
})
export class AppComponent {
  grid = {
    rowKey: 'id',
    dataTypes: { partNumber },
    columns: columns,
    rows: rows,
    state: { sort: [{ col: 'part', dir: 'asc' }] },
  };
}

bootstrapApplication(AppComponent);