View as Markdown

Foreign Key

Lookup columns

Foreign-key columns that store a key and show, sort, filter, search and group by a looked-up value, from a local table or a cached remote source.

A lookup column (a foreign-key column) holds a key in the row and displays a value from another table:

const customers = [{ id: 1, name: 'Aiko Tan' }, { id: 2, name: 'Bao Nguyen' }];

const columns: DataGridColumn<Order>[] = [
  { field: 'customerId', header: 'Customer', lookup: { data: customers, keyField: 'id', displayField: 'name' } },
];

keyField is the field of the lookup rows matched against the cell value (default: the column field); displayField is the field shown. Both accept dot paths. All lookup settings live in column.lookup. The React DataGrid registers the lookup module automatically as soon as a column declares lookup; there is nothing to import or inject.

The display text is what the user sees, so it is also what the grid works with: sorting orders by the name, not the key; the filter row, the filter menus, checkbox facets and search match the name; and grouping groups by the name. The row itself keeps the key, so api.getViewRows() returns rows with the raw value. The display text feeds the dictionary-encoded sort keys, so a warm re-sort or re-filter of a lookup column runs no lookups.

To list the lookup columns, filter the compiled columns: those returned by api.getColumns() carry lookup on lookup columns, so api.getColumns().filter((c) => c.lookup) returns them. A dedicated method would grow the API without adding a capability.

Export (CSV, Excel, PDF) and the clipboard module's copy() write the display text, never the key; an unknown key, or a remote key still loading, exports blank. Pasting either the key or the display text into a lookup cell stores the key (Clipboard). Copying an edited lookup cell copies the display text of its draft key.

Lookup columns

Open demo

Two local lookup (foreign-key) columns - cells hold keys and show names; sorting, filtering, search and grouping run on the display text; the lookup columns are listed from api.getColumns().

Drag a column header here to group by that column
Order
Customer
Department
Product
Amount
ORD-10000
Aiko Tan
Operations
Oolong tea
$75.86
ORD-10008
Aiko Tan
Human resources
Oolong tea
$22.95
ORD-10016
Aiko Tan
Engineering
Grinder
$348.30
ORD-10024
Aiko Tan
Marketing
Kettle
$496.49
ORD-10032
Aiko Tan
Finance
Filter beans
$107.10
ORD-10040
Aiko Tan
Operations
Pour-over kit
$340.20
ORD-10048
Aiko Tan
Human resources
Pour-over kit
$285.60
ORD-10056
Aiko Tan
Engineering
Grinder
$438.60
ORD-10064
Aiko Tan
Marketing
Oolong tea
$140.25
ORD-10072
Aiko Tan
Finance
Grinder
$1,419.00
ORD-10080
Aiko Tan
Operations
Grinder
$1,290.00
ORD-10088
Aiko Tan
Human resources
Espresso beans
$33.30
ORD-10096
Aiko Tan
Engineering
Grinder
$696.60
ORD-10104
Aiko Tan
Marketing
Green tea
$43.94
ORD-10112
Aiko Tan
Finance
Kettle
$525.69
ORD-10120
Aiko Tan
Operations
Kettle
$454.30
ORD-10128
Aiko Tan
Human resources
Filter beans
$14.00
ORD-10136
Aiko Tan
Engineering
Filter beans
$39.90
ORD-10144
Aiko Tan
Marketing
Oolong tea
$12.11
ORD-10152
Aiko Tan
Finance
Oolong tea
$36.34

Type "fin" in the Department filter cell and press Enter: the filter matches the display text.

Server-side sources

The display-text ordering above is what the grid does when it holds the rows (a local array or an offline source). With a server-side source (for example createJsonDataSource), sorting and grouping run on the server, and the grid sends a lookup column like any other column: by its field, which holds the key (through fieldOf when set). Sorting the customerId lookup column sends sort: [{ field: 'customerId', dir: 'asc' }], and grouping by it sends group: [{ field: 'customerId' }]; the lookup table never travels with the query.

A server that sorts the raw key orders the rows by key, not by name. To order by the display text, as the grid does locally, the server joins the lookup table and sorts by the display value; for grouping it orders the groups by the display value and keeps the key as the group key, which the grid shows through the lookup. The reference handler does this join when told which fields hold lookup keys:

import { createJsonQueryHandler } from '@salmonui/data/server';

createJsonQueryHandler(orders, {
  key: 'id',
  lookups: { customers },
  lookupFields: { customerId: { lookup: 'customers', keyField: 'id', displayField: 'name' } },
});

A SQL backend does the same with a join (ORDER BY customers.name, orders.customer_id).

Remote lookups

lookup.data can also be a LookupSource, an object with load({ keys, keyField, displayField, signal }) that returns the lookup rows of the requested keys. The grid asks only for the keys it needs, batched per microtask, and re-sorts or re-filters when they arrive. @salmonui/data/lookup ships two cached sources:

  • createLookup({ load, keyField?, ttl? }) wraps any loader with a per-key cache: cached and known-missing keys never reach the loader, identical in-flight keys are joined, and entries expire after ttl ms.
  • createJsonLookup({ url, name, ttl? }) loads over the JSON protocol endpoint (createJsonQueryHandler(rows, { lookups: { [name]: rows } }) on the server).

One lookup can back several columns and several grids. While a remote lookup column is loading, its header carries data-lookup-status="loading" and aria-busy="true" with a status indicator labelled by the lookupLoading text; a failed load shows data-lookup-status="error" (lookupError). Remote lookups display as strings.

Remote lookup

Open demo

A lookup column backed by a cached @salmonui/data createLookup source with a simulated 900 ms server; the missing keys load in one batched request while the header shows the load status.

Order
Supplier
Product
Amount
ORD-10000
Grinder
$219.30
ORD-10001
Pour-over kit
$42.00
ORD-10002
Filter beans
$79.80
ORD-10003
Green tea
$92.50
ORD-10004
Pour-over kit
$428.40
ORD-10005
Grinder
$490.20
ORD-10006
Pour-over kit
$319.20
ORD-10007
Espresso beans
$66.60
ORD-10008
Green tea
$41.63
ORD-10009
Grinder
$1,290.00
ORD-10010
Pour-over kit
$428.40
ORD-10011
Green tea
$62.90
ORD-10012
Green tea
$31.45
ORD-10013
Filter beans
$168.00
ORD-10014
Green tea
$64.75
ORD-10015
Grinder
$387.00
ORD-10016
Kettle
$700.92
ORD-10017
Filter beans
$88.20
ORD-10018
Grinder
$1,096.50
ORD-10019
Oolong tea
$76.50
No events yet.

API reference

ColumnLookup lists the lookup options and LookupSource the remote contract; createLookup, createJsonLookup and createJsonQueryHandler cover the cached sources and the server join.