Server-side windowed data (recipe)
For hundreds of thousands of rows: the component only computes which slice it needs; fetching, cancelling and merging are the host's job. This page gives a four-step recipe plus a runnable example (
oas-tabledemoed; gantt / resource / tree / list share the contract — differences below).
Contract
- Attributes:
windowed(enable) +total(full row count) +window-start(absolute row index ofdata[0]);datacarries only[window-start, window-start + data.length). - Event:
oas-window-requestwithdetail: { start, end, reason, visualStart?, visualEnd? }(half-open, absolute row indices;reason=init/scroll/resize/data). The same expected window is never emitted twice; it resets after the window is covered.start/endare always DATA coordinates; when the slice contains inline expand rows (or the visual protocol is active) the optionalvisualStart/visualEndcarry the visible window's visual coordinates, and the range has already been mapped through the visual slots back to data coordinates. Timing: inside a scroll frame the request is dispatched BEFORE the tbody rebuild (the authoritative scroll position is passed through directly, preventing clamp miscalculations during the clear) — a listener that reads the DOM synchronously sees the previous frame's render; the refeed drives a re-render via attribute changes. - Prerequisite: a measurable scroll viewport (
heightor a measurablemax-height); otherwise no request is emitted (onedevWarn). A windowed table slice must be flat (children are not recursed).
Four steps
- Fetch by segment: align requests to fixed segments (e.g. 200 rows) so one response covers many windows — typically an order of magnitude fewer requests.
- Keep only the latest: abort in-flight fetches (
AbortController) or drop stale responses with a request sequence number; otherwise an old response arriving late makes the view jump back. - Merge by id into the full dataset: edits/
oas-tasks-changecarry only the slice; the host must merge by id and never replace the full dataset wholesale. - Refill atomically: write
datafirst, thenwindow-start(within the same synchronous step) to avoid a "new slice + old start" intermediate state.
// Real project: segment fetch + latest-only + atomic refill
let seq = 0
let controller: AbortController | null = null
const SEG = 200
el.addEventListener('oas-window-request', async (event) => {
const { start, end } = (event as CustomEvent<{ start: number; end: number }>).detail
const my = ++seq
controller?.abort()
controller = new AbortController()
const segFrom = Math.floor(start / SEG)
const segTo = Math.floor((Math.max(start, end - 1)) / SEG)
const from = segFrom * SEG
const to = Math.min(total, (segTo + 1) * SEG)
const rows = await fetch(`/api/rows?from=${from}&to=${to}`, {
signal: controller.signal,
}).then((r) => r.json())
if (my !== seq) return // stale response, drop it
el.setAttribute('window-start', String(from))
el.setAttribute('data', JSON.stringify(rows))
})Visual protocol recipe (inline expand rows / slices starting after expanded rows)
When the slice contains inline expand content (row.expand), or expanded rows already exist before the slice start (the "cross-slice" case), the event start / end is only the component's clamped projection over the loaded slice — when the visible window and the slice do not intersect, it fetches rows that are not actually visible. In that case adopt the visual protocol: the host owns the real expansion layout (single source), tells the component the total visual row count and the slice's first visual row via visual-total / visual-start, and fetches by preferring the event's visualStart / visualEnd.
Host-side recipe (on top of the four steps):
- Single-source layout: which rows carry expansion slots across the full dataset (
visualTotal= data rows + expansion slots) and the slice's first visual row (visualStart= expansion slots before that data row) are maintained in one host-owned place. Once the protocol is active, expansion is fully host-controlled — the component's local toggle only emitsoas-expandwithout modifyingexpanded; the host listens and writes backexpanded/data/ visual attributes to re-render. Never double-write from both sides. - Prefer the visual fields for fetching: when the event carries
visualStart/visualEnd, map them back to a data range (intersecting visual slots → owner data rows);createWindowedSourcetakes the union of the mapped range and the event's data range (the eventstart/endis computed by the component and always carries the keyboard target row and the viewport margin), so the host mapping may cover only the visible rows without its own padding. - Refill visual attributes atomically: after the fetch completes, write
window-start+visual-start+datawithin the same synchronous step (one snapshot), thentotal+visual-total— the component re-renders with the new visual coordinates only after the data is ready, with no "new slice + old visual start" intermediate state. - Re-evaluate on refill: the host may change the expansion layout between request and response (expansion count changes move the visual coordinates) — the refill must use the latest snapshot at response time, never the stale request-time values.
// Visual adapter over a real layout: one expansion slot every 25 rows (host-known)
const EXPANDED = new Set([0, 25, 50, 75, 100])
const DATA_TOTAL = 10000
const slotOf = (i: number) => i + [...EXPANDED].filter((e) => e < i).length
const adapter = {
// Called at init and on every refill: return the current snapshot (compute it fresh; never cache request-time values)
getGeometry(dataStart: number) {
return { start: slotOf(dataStart), total: DATA_TOTAL + EXPANDED.size }
},
// Map the visible window's visual range back to a data fetch range (covering just the visible gap is enough:
// the helper unions the result with the event's data range, which already carries the keyboard target and
// viewport margin; widen it yourself only if you want a wider fetch)
getDataRange(visualStart: number, visualEnd: number) {
let lo = Infinity
let hi = -Infinity
for (let i = 0; i < DATA_TOTAL; i++) {
const s = slotOf(i)
const len = EXPANDED.has(i) ? 2 : 1
if (s + len <= visualStart || s >= visualEnd) continue
if (i < lo) lo = i
if (i + 1 > hi) hi = i + 1
}
return { start: Math.max(0, lo - 8), end: Math.min(DATA_TOTAL, hi + 8) }
},
}To skip the glue code, createWindowedSource from @oas-ui/ui/data/table/advanced accepts this adapter directly: it re-evaluates the geometry at init and on every refill and writes visual-start / visual-total for you, and maps requests through getDataRange automatically — the mapped range is unioned with the event's data range (the event start / end always carries the keyboard target row and the viewport margin; the mapping only fills the visible-row gap, so the host adapter does not need its own padding). An invalid mapping (must be finite with end > start) or invalid geometry (non-finite, start<0, total≤0, start>total, total< data total) fetches nothing / writes no wrong visual attributes and dispatches one oas-window-error (detail carries the visual range and the original data range). After a geometry failure the table falls back to the local model, and subsequent event visual fields are local-scope only — the helper stops mapping (fetching by the event's start / end verbatim). Mapping and geometry failures share the same recovery semantics: a new-window event retries; for the same window call source.refresh() to force a refetch — the table's winReq same-window dedupe and the loaded-coverage check may swallow a same-window retry. Without visual the helper writes no visual attributes at all — behavior is identical to before. One source instance per element: creating a second source on the same element doubles listeners and fetches (warns once per duplicate creation and still returns the new instance); call destroy() first and rebuild (rebuilding after destroy() is warning-free).
In non-virtual windowed mode (no
height) the protocol only affects "expansion is host-controlled", not the layout: there is no scrollable viewport, andvisual-start/visual-totalonly govern who owns expansion control.
Runnable example (local mock server)
The demo simulates a server with local data: 200 rows per segment, 120ms latency, per-segment cache and latest-only. Scroll the table and watch the counters — scrolling within a loaded segment issues no new request (one response covers many windows).
windowed + segment cache + latest-only (mock 50k rows)
Waiting for the first window request…
Gantt / Resource windowed (host-managed axis)
Beyond row windows, timeline components support a host-managed axis via range-start / range-end; when the visible time window approaches an axis edge, oas-range-request is emitted so the host can extend the range. The two demos below show "segmented rows + extend the axis on demand".
Gantt (8000 tasks / 1-year axis / segments of 200)
gantt windowed + auto-extend the axis at the edge
Waiting for the first window request…
Resource (400 resources / half-year axis / segments of 50)
resource windowed + auto-extend the axis at the edge
Waiting for the first window request…
React / Vue host wrappers
Custom-element bridging notes: Vue needs isCustomElement: (tag) => tag.startsWith('oas-') in the compiler options; React 19+ supports custom elements natively (below 19, use a ref + addEventListener). Do not write attributes synchronously inside the event handler (it re-enters update) — refill after the fetch resolves.
React
import { useEffect, useRef } from 'react'
const SEG = 200
export function WindowedTable({ total }: { total: number }) {
const ref = useRef<HTMLElement | null>(null)
useEffect(() => {
const el = ref.current
if (!el) return
let seq = 0
let controller: AbortController | null = null
const onRequest = async (event: Event) => {
const { start, end } = (event as CustomEvent<{ start: number; end: number }>).detail
const my = ++seq
controller?.abort()
controller = new AbortController()
const from = Math.floor(start / SEG) * SEG
const to = Math.min(total, from + SEG)
const rows = await fetch(`/api/rows?from=${from}&to=${to}`, { signal: controller.signal })
.then((r) => r.json())
.catch(() => null)
if (!rows || my !== seq) return
el.setAttribute('data', JSON.stringify(rows))
el.setAttribute('window-start', String(from))
}
el.addEventListener('oas-window-request', onRequest)
return () => {
controller?.abort()
el.removeEventListener('oas-window-request', onRequest)
}
}, [total])
return (
<oas-table
ref={ref}
windowed=""
total={String(total)}
window-start="0"
height="320"
row-key="id"
columns={JSON.stringify([{ key: 'id', title: 'ID' }, { key: 'name', title: 'Name' }])}
/>
)
}TypeScript needs JSX intrinsic-element augmentation for custom elements (or
// @ts-expect-erroron the tag).
Vue 3
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
const SEG = 200
const table = ref<HTMLElement | null>(null)
let seq = 0
let controller: AbortController | null = null
async function onWindowRequest(event: Event) {
const el = table.value
if (!el) return
const { start, end } = (event as CustomEvent<{ start: number; end: number }>).detail
const my = ++seq
controller?.abort()
controller = new AbortController()
const from = Math.floor(start / SEG) * SEG
const to = Math.min(50000, from + SEG)
const rows = await fetch(`/api/rows?from=${from}&to=${to}`, { signal: controller.signal })
.then((r) => r.json())
.catch(() => null)
if (!rows || my !== seq) return
el.setAttribute('data', JSON.stringify(rows))
el.setAttribute('window-start', String(from))
}
onMounted(() => table.value?.addEventListener('oas-window-request', onWindowRequest))
onBeforeUnmount(() => {
controller?.abort()
table.value?.removeEventListener('oas-window-request', onWindowRequest)
})
</script>
<template>
<oas-table
ref="table"
windowed
total="50000"
window-start="0"
height="320"
row-key="id"
:columns="JSON.stringify([{ key: 'id', title: 'ID' }, { key: 'name', title: 'Name' }])"
/>
</template>In templates you can also write
@oas-window-request="onWindowRequest"(Vue 3 forwardsoas-window-requestas a custom event).
Component differences
| Component | windowed semantics | Extra prerequisites |
|---|---|---|
oas-table | row slice; total reused as the full row count | height; flat slice; local sort/filter/group/pagination are the host's job |
oas-gantt | row slice | height; flat slice; range-start/range-end for a host-managed axis; auto-schedule/critical path/row reorder skipped |
oas-resource | resource-row slice | height; range-start/range-end for a host-managed axis; load stats cover the loaded slice only |
oas-tree | root-node slice (children travel with the root) | height; local filter/check-all skipped; refill by widening |
oas-list | row slice | height or a measurable max-height; list semantics are owned by the component (no host role needed) |
gantt / resource also emit oas-range-request (detail: { startMs, endMs, reason }) when the visible time window approaches an axis edge — follow the same "latest-only + merge" rules.
Common pitfalls
- Refilling less than the expected window: the component keeps requesting. Return a segment covering
[start, end)(or a superset). - Replacing the whole dataset: edit write-backs (
oas-tasks-change) contain only the slice; a wholesale replace loses unloaded rows. - Forgetting
window-start: data and absolute indices drift (wrong rows rendered, wrong ARIA indices). - Fetching with only
start/endwhile the slice contains expansions: when the visible window and the slice do not intersect (expanded rows before the slice start),start/endis only a clamped guess — adopt the visual protocol and prefervisualStart/visualEnd(see the recipe above). - Double-writing expansion under the protocol: once
visual-total/visual-startare active, expansion is fully host-controlled (the local toggle only emitsoas-expand); if the host does not listen and write back, expanding "does nothing". Expansion count changes also move the visual coordinates — the refill must re-evaluatevisual-start/visual-totalinstead of reusing request-time values. - Visual attributes out of sync with the data: write
visual-startin the same synchronous step aswindow-start/data(one snapshot), thentotal/visual-total— splitting the writes creates a "new slice + old visual start" intermediate state that flashes misaligned rows while scrolling. - Writing other attributes synchronously in the handler (e.g.
loading): re-enters update (scrollTop may be transiently clamped to 0 mid-render, causing extra requests). Refill after fetching; if you need a loading state, set it in a microtask or after the fetch resolves. - Fetching per window: request count grows linearly with scrolling; segment fetching cuts it by an order of magnitude.
- No
height: no requests are emitted (no scroll viewport) — not a bug, the viewport must exist first. - Property writes before upgrade: before assigning properties such as
el.tasks/el.resourcesfrom page scripts,await customElements.whenDefined('oas-gantt')(or the matching tag) — on an unupgraded element the property becomes a plain own field that shadows the upgraded setter, silently losing data (the demos on this page already do this; the attribute channel avoids the issue).