Skip to content

context

What the application knows that the rows do not — the user’s role, today’s prices — handed to the grid’s callbacks.

Typeunknown
Defaultnot set
Live update (update())Yes
Angularthe [context] input
<dz-grid>the settings property: el.settings = { context: … }
Declared inpackages/dom/src/settings.ts

Any value, which the grid never reads: it hands it on. The column options editable, validate, tooltip and an editor’s options get it as an argument; so do rowClass, cellClass, every part, the selection’s canSelect and the export’s canExport. One rule can then say « a manager may edit this column » without a closure over global state.

A new value is a new identity: give a new object, not the same one changed. With update({ context }), what depends on it is asked again — the rows’ classes, which cells may be edited.

import { createGrid } from '@dzgrid/dom';
import { EN } from '@dzgrid/dom/locales/en';
type Shift = { id: string; nurse: string; ward: string };
type Ctx = { role: 'manager' | 'staff' };
const isManager = (context: unknown) => (context as Ctx).role === 'manager';
const grid = createGrid<Shift>(document.getElementById('shifts')!, {
rows: [{ id: 's1', nurse: 'A. Moreau', ward: 'North' }],
columns: [
{ id: 'nurse', header: 'Nurse' },
{ id: 'ward', header: 'Ward', editable: (_row, context) => isManager(context) },
],
getRowId: (s) => s.id,
messages: EN,
context: { role: 'staff' } satisfies Ctx,
});
grid.update({ context: { role: 'manager' } satisfies Ctx }); // the ward column's rule now says yes

Editing a cell needs the edit features: see Editing.

  • rowClass — classes from the row and the context.
  • Editing — editable and validate.