- Data Grid docs
- Data
Live data
Apply your application's own updates, added and removed records to the grid as they arrive, batch a fast feed, and flash the cells that changed.
A price feed, a queue of jobs, a dashboard of orders coming in: the records change while the user looks at them. Transactions are your application's own channel for those changes. They update, add and remove local records in one step, keep the user's search, filters and sort, and can flash the cells that changed so the eye catches them.
Open the live example: prices changing twice a second
In the grid
Nothing to do: changed values appear in place, records added by a transaction take their place in the current sort and filters, and removed ones go. With flashChanges: true, each changed cell lights up for a moment. When the system asks for reduced motion, the cell gets a static outline instead of the flash.
From code
| Method or option | Does |
|---|---|
applyTransaction({ add, update, remove }) | Applies added records, updates ({ id, values }) and removed IDs to local records in one step; resolves with { added, updated, removed } |
applyTransactionAsync(tx) | Collects transactions for 50 ms and applies them as one; each call resolves with the combined result |
flashChanges: true | Flashes the cells a transaction changes |
flashCells([{ id, field }]) | Flashes cells, for data the grid didn't change itself, such as a server source after refresh() |
A transaction is not a user's edit:
- It works on read-only records and read-only fields, so a grid can show prices the user can't type over.
- It doesn't go to the undo history, and it doesn't fire
beforeEdit,beforeAddorbeforeRemove. - It applies all of it or nothing: an update of an unknown record or of a calculated field changes nothing and rejects. IDs in
removethat no record has are skipped. - It resolves with
added, the IDs of the added records,updated, the number of cells whose value changed, andremoved, the number of records removed. - The
changeevent reports it afterwards as{ action: 'transaction', add, update, remove, ids, source: 'api' }, whereidsare the IDs the added records got.
applyTransactionAsync is for feeds that send many small messages: instead of a redraw per message, the grid redraws once per 50 ms batch. Use applyTransaction when your code waits for each change to be shown.
JavaScript
import { mountTable } from '@8sheets/data-grid';
const grid = await mountTable(document.querySelector('#prices'), {
title: 'Prices',
fields: [
{ name: 'Symbol', type: 'string' },
{ name: 'Price', type: 'number', format: '#,##0.00', readOnly: true },
{ name: 'Change', type: 'number', format: '+0.00%;-0.00%;0.00%', readOnly: true },
],
data: initialPrices, // the record IDs are their positions: 0, 1, 2, …
flashChanges: true,
density: 'compact',
});
// Each message updates a few records; the grid draws once per 50 ms batch.
const feed = new WebSocket('wss://example.com/prices');
feed.onmessage = (e) => {
const { updates } = JSON.parse(e.data); // [{ id: 3, Price: 101.5, Change: 0.012 }, …]
grid.applyTransactionAsync({ update: updates.map(({ id, ...values }) => ({ id, values })) });
};
// A listing and a delisting in one step.
const { added } = await grid.applyTransaction({ add: [{ Symbol: 'NEW', Price: 10, Change: 0 }], remove: [12] });React
import { useEffect, useMemo, useRef } from 'react';
import { DataGrid } from '@8sheets/data-grid-react';
export function Prices({ initialPrices, feed }) {
const ref = useRef(null);
// `data` only at mount: later changes go through transactions, so the grid keeps its view.
const options = useMemo(() => ({ title: 'Prices', data: initialPrices, flashChanges: true }), []);
useEffect(() => {
const onMessage = (update) => ref.current?.grid && ref.current.applyTransactionAsync({ update });
feed.subscribe(onMessage);
return () => feed.unsubscribe(onMessage);
}, [feed]);
return <div style={{ height: 480 }}><DataGrid ref={ref} options={options} /></div>;
}Vue
<script setup>
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { DataGrid } from '@8sheets/data-grid-vue';
const props = defineProps({ initialPrices: Array, feed: Object });
// `data` only at mount: later changes go through transactions, so the grid keeps its view.
const options = { title: 'Prices', data: props.initialPrices, flashChanges: true };
const dataGrid = ref(null);
const onMessage = (update) => dataGrid.value?.grid && dataGrid.value.applyTransactionAsync({ update });
onMounted(() => props.feed.subscribe(onMessage));
onBeforeUnmount(() => props.feed.unsubscribe(onMessage));
</script>
<template>
<div style="height: 480px"><DataGrid ref="dataGrid" :options="options" /></div>
</template>Angular
import { Component, Input, OnDestroy, ViewChild } from '@angular/core';
import { DataGridComponent, DataGridOptions, MountedDataGrid } from '@8sheets/data-grid-angular';
import { Subscription } from 'rxjs';
import { PriceFeed } from './price-feed.service';
@Component({
selector: 'app-prices',
imports: [DataGridComponent],
template: `<eight-data-grid #grid [options]="options" (ready)="start()" style="height: 480px" />`,
})
export class Prices implements OnDestroy {
@ViewChild('grid') grid!: DataGridComponent;
@Input() options: DataGridOptions = { flashChanges: true };
private sub?: Subscription;
constructor(private feed: PriceFeed) {}
start() {
this.sub = this.feed.updates$.subscribe((update) => this.grid.applyTransactionAsync({ update }));
}
ngOnDestroy() {
this.sub?.unsubscribe();
}
}In the wrappers, a new data array loads new records and starts the view over. For a feed, pass data once and send the changes as transactions.
With a server source
The records live on your server, so there is nothing local to change: applyTransaction rejects. Update the server, call refresh() to fetch the rows in view again, and flash what changed with flashCells:
socket.onmessage = async (e) => {
const changed = JSON.parse(e.data); // [{ id: 'A-1', field: 'Price' }, …]
await grid.refresh();
grid.flashCells(changed);
};
Limits
- Transactions change local records; a server source refreshes instead.
- A pivot is read-only: go back to the records before applying a transaction.
- Flashing marks cells on screen; it doesn't change their value or style in exports.