Data Binding
Data sources
Local arrays, the Salmon UI JSON protocol, offline, live and application-controlled data sources.
The grid reads rows through one structural data-source contract. A local array is the simplest source; every other source implements the same contract, including user-written ones. The current query (sorting, filters, search, grouping, aggregates, paging and expansion) is a serializable descriptor, and a grid can start from an initial query (see Initial query).
import { DataGrid } from '@salmonui/data-grid';
import { createJsonDataSource } from '@salmonui/data';
const orders = createJsonDataSource<Order>({ url: '/api/orders', key: 'id', fields: ['id', 'customer', 'total'] });
<DataGrid data={orders} columns={columns} sorting paging aria-label="Orders" />;
data takes either an array (reactive: a new array replaces the rows) or a
DataSource, which is read once on mount.
Remote: the Salmon UI JSON protocol
Remote sources speak one JSON protocol over a pluggable transport.
The protocol does not send one object per row: a { result, count }
shape costs payload and per-row decode
work, while tuples with dictionary encoding measure about a third of its
payload and decode time. To use an existing endpoint, expose one that
speaks the protocol, or give createJsonDataSource a transport that calls
the existing endpoint and maps its rows to tuple rows and total.
Rows travel as tuples with optional dictionaries; sorting, filtering and
grouping run on the server; results can include server-side
aggregates. Every request carries an abort signal and a version
stamp, so a stale response never overwrites a newer one. With
infinite paging the source loads blocks on demand and keeps a bounded block
cache.
The demo endpoint runs the reference handler from @salmonui/data/server
(createJsonQueryHandler) over an in-memory table. Salmon UI ships no
Entity Framework, Dapper or SQL server samples: the contract is the wire protocol, not a server stack, so a
real backend ports the handler's query semantics to its ORM or SQL, or the
client supplies a custom transport.
Remote JSON data source
Open demo50,000 orders from a Salmon UI JSON protocol endpoint (the reference handler, behind a Next.js route handler or in the browser on the static site), loaded in infinite blocks, sorted on the server with an initial sort.
Sorting runs on the server: click a header and watch the requests.
Request and response hooks
createJsonDataSource takes three hooks, each called with the request
kind (read, prefetch, group, tree, facet or write) and the
protocol request:
onRequest(event)runs before a request is sent. Returningfalsecancels it: the read rejects withRequestCanceledErrorand nothing is sent.onResponse(event)runs with every successfulresponse(and itsdurationMs) before the source decodes it into rows. It can observe or transform: returning a response object replaces the response for that request, so it can rewrite rows, dictionaries,totalor aggregates; returning nothing (or any non-object) keeps the server response. Return a new object instead of mutatingevent.response: the cache keeps the server response, and a cache hit runs the hook again.onError(event)runs when a request fails (not when it is aborted).
const source = createJsonDataSource<Order>({
url: '/api/orders',
key: 'id',
onRequest: (e) => e.kind !== 'prefetch' || navigator.onLine,
onResponse: (e) =>
e.kind === 'read' && 'rows' in e.response
? { ...e.response, rows: e.response.rows.filter((row) => row[0] !== null) }
: undefined,
});
The grid-level onViewChange fires after the new
rows are rendered, for every source, with a reason of data, sort,
filter, search, group, page or refresh. The hooks above belong to
the JSON source only; the other sources need none: a controlled source
already returns the rows from onQueryChange, a local array is the data,
and a custom transport can rewrite responses for any protocol. The demo
logs every request and response.
Other protocols: a custom transport
@salmonui/data has no built-in Web API, OData or GraphQL client:
.NET request shapes, an OData encoder, parser and
$batch writer, or a schema-specific GraphQL query builder would ship in
every bundle that imports @salmonui/data, against its 25 KB budget.
createJsonDataSource takes a transport
function instead of a url: it receives each protocol request (query
with sort, where, search, page, select) and resolves with a
protocol response (fields, tuple rows, total). A transport that maps
the query to $orderby / $skip / $top / $count and the OData
value back to tuples is an OData bridge; the same function can post the
request as a GraphQL variable or call a Web API action (for OData v2/v3,
read total from $inlinecount; an OData $batch submit becomes one
write request). Paging, caching, abort and stale-response handling stay in
the data source.
OData bridge transport
Open demoA user-written transport maps Salmon UI protocol reads to an OData-style GET ($orderby, $skip, $top, $count, $select) and the OData body back to tuples; the same pattern bridges Web API, OData and GraphQL backends.
Sort a column or change the page: each request below is what the transport sent to the OData-style endpoint.
Initial query
There is no separate query object prop: each feature owns its
state as a reactive options prop with before and after events, and a second
query builder would duplicate that state, conflict with it and add a query
DSL to the core bundle. The initial sorting.sortBy, filtering.filters,
search.text, grouping.groupBy and paging.page / pageSize form the
initial query, which is sent to remote sources on the first read. Remote
column selection is the source's fields; extra request parameters
(addParams) go through its headers or a custom transport.
<DataGrid
data={orders}
columns={columns}
filtering={{ filters: [{ field: 'Status', operator: 'equal', value: 'Open' }] }}
sorting={{ sortBy: [{ field: 'OrderDate', direction: 'desc' }] }}
paging={{ pageSize: 20 }}
/>
Remote editing
With editing, saves go to dataSource.write: a JSON source sends one
{ v: 1, write } request per save with the added rows, the changed cells
of each row and the removed keys. Batch mode collects every
pending change into that single request. A rejected write keeps the
pending changes as drafts with the error message as their row error, so
the user can retry the save; row and cell errors returned by the server
mark only the affected rows and cells.
Remote editing
Open demoBatch edits, additions and deletions saved to a remote source as one protocol write request; a failed write keeps the pending changes.
Edit cells, add or delete rows, then Save: all pending changes go to the server as one write request.
Offline
An offline source loads a full array once, then queries it locally.
Offline data source
Open demoLoads a fetched array once and runs sorting and filtering locally.
Loading…
Live streams
A live source combines a snapshot with a stream of patches (upserts, removals, appends) applied by row id, so updates do not refresh the whole grid.
Live data stream
Open demoA snapshot plus a stream of upsert patches applied by row id without a full refresh; sorting stays live.
The first 40 orders receive patches every 0.8 s; sort by Amount to see rows move.
Silent updates through the API
api.setCellValue(rowId, field, value) and api.setRowData(rowId, row)
change one cell or one row in place, without the edit lifecycle (no
onBeforeCellValueChange, no validation, no dirty mark) and without
refreshing the grid: only the touched row re-renders. Both
return a DataGridActionResult. They need no editing prop: they replace
the row of a local source, and the demo grid has no editing at all.
Silent cell and row updates
Open demosetCellValue and setRowData change one cell or one row in place without the edit lifecycle and without a full refresh; only the touched row re-renders.
Application-controlled
A controlled source hands each query to the application, which returns the
rows. Saves go to onDataChange with the same WriteRequest
a remote source receives; the application persists
them and returns a WriteResult. Both are options of
createControlledDataSource, not DataGrid props. When the write settles,
the grid shows the rows the application applied (the rows echoed in the
WriteResult, else the request rows) without a re-read; rows the result
reports as failed keep their previous values.
The controlled source is also the integration point for state libraries:
Salmon UI ships no Redux sample (state library samples are not
library features); keep the rows in the store and answer onQueryChange and onDataChange from it, or
pass the store's rows to data and the query to the controlled feature
props (sorting.sortBy, filtering.filters, paging.page).
Controlled data operations
Open demoThe grid hands each query to the application, which runs it and returns rows (onQueryChange), and each save, which the application persists (onDataChange); the serialized query and the writes are shown.
Sort a column: the serialized query below is what the application received. Double-click a cell and edit it: the save goes to onDataChange.
(waiting for the first query)
Empty state
With no rows the body shows one empty row. renderEmpty({ reason })
replaces its content; reason is 'noData' when the source has no rows
and 'noMatch' when the query filters every row out. Without it the grid
shows localeText.noRows.
Empty state
Open demorenderEmpty tells an empty data set (noData) from a query without matches (noMatch); without it the grid shows localeText.noRows.
Loading indicator and refresh
While the source loads, an overlay covers the body: a spinner by default,
or skeleton rows with loadingIndicator: 'skeleton'. The overlay
element is .salmon-data-grid-loading (the loading part of classNames and
styles) and is aria-hidden; the grid root carries aria-busy, and the
polite live region announces "Loading" after 400 ms and "Loaded {n} rows"
once the rows arrive. The empty-state row stays hidden while the source
loads. renderLoading({ phase }) replaces the overlay content; phase is
'initial' before the first result, 'page' while a page change loads and
'refresh' otherwise. api.setLoading(true) / api.setLoading(false)
show and hide the indicator by hand; a data load in progress shows it
regardless.
api.refresh() re-reads the data
with the current query and keeps sorting, filters, grouping, the page, the
selection and the scroll position. A local array mutated in place renders
its new values; a remote source is re-read past its cache. onViewChange
reports the result with reason: 'refresh', and while an asynchronous read
is in flight the indicator shows with phase 'refresh'. There is no
refreshColumns(): columns are a prop.
Loading indicator and refresh
Open demoThe spinner or skeleton overlay while a slow source loads, renderLoading with its initial, page and refresh phases, setLoading(true/false), and refresh() re-reading the data with the current query.
Every read takes 700 ms. Change the page to see the indicator; refresh() re-reads the current page.
API reference
The sources are createJsonDataSource,
createOfflineDataSource,
createLiveDataSource and
createControlledDataSource
from @salmonui/data; the reference handler is
createJsonQueryHandler
from @salmonui/data/server. A custom source implements
DataSource.