Highlight changes by flashing or animating cells.
import {
CellStyleModule,
ClientSideRowModelModule,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowApiModule,
CellStyleModule,
ClientSideRowModelModule,
HighlightChangesModule,
]);
let gridApi: GridApi;
let updateInterval: ReturnType<typeof setInterval>;
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
const gridOptions: GridOptions = {
columnDefs: [
{ field: "a", enableCellChangeFlash: true },
{ field: "b", enableCellChangeFlash: true },
{ field: "c", cellRenderer: "agAnimateShowChangeCellRenderer" },
{ field: "d", cellRenderer: "agAnimateShowChangeCellRenderer" },
{ field: "e", cellRenderer: "agAnimateSlideCellRenderer" },
{ field: "f", cellRenderer: "agAnimateSlideCellRenderer" },
],
defaultColDef: {
flex: 1,
cellClass: "align-right",
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
onGridReady: () => {
const updateValues = () => {
// The timer outlives the grid, so stop once it is gone rather than calling a destroyed API.
if (gridApi!.isDestroyed()) {
clearInterval(updateInterval);
return;
}
const rowCount = gridApi!.getDisplayedRowCount();
// pick 2 cells at random to update
for (let i = 0; i < 2; i++) {
const row = Math.floor(window.agRandom() * rowCount);
const rowNode = gridApi!.getDisplayedRowAtIndex(row)!;
const col = ["a", "b", "c", "d", "e", "f"][
Math.floor(window.agRandom() * 6)
];
rowNode.setDataValue(col, Math.floor(window.agRandom() * 10000));
}
};
updateInterval = setInterval(updateValues, 250);
},
};
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 145045) % 10000),
b: Math.floor(((i + 323) * 543020) % 10000),
c: Math.floor(((i + 323) * 305920) % 10000),
d: Math.floor(((i + 323) * 204950) % 10000),
e: Math.floor(((i + 323) * 103059) % 10000),
f: Math.floor(((i + 323) * 468276) % 10000),
});
}
return rowData;
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
.align-right {
text-align: right;
}
<div id="myGrid" style="height: 100%"></div>
The example above shows changing values:
- Columns A and B use Flashing Cells.
- Columns C and D use the Animate Show Change cell renderer.
- Columns E and F use the Animate Slide Cell cell renderer.
Flashing Cells Copy Link
You can trigger cells to flash either though the Grid API or by enabling cells to flash when the data changes.
Enable Flashing on Data Change Copy Link
Set Column attribute enableCellChangeFlash=true to flash the cells when data changes.
import {
CellStyleModule,
ClientSideRowModelModule,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowApiModule,
HighlightChangesModule,
CellStyleModule,
ClientSideRowModelModule,
]);
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: [
{ field: "a" },
{ field: "b" },
{ field: "c" },
{ field: "d" },
{ field: "e" },
{ field: "f" },
],
defaultColDef: {
flex: 1,
cellClass: "align-right",
enableCellChangeFlash: true,
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
};
function onUpdateSomeValues() {
const rowCount = gridApi!.getDisplayedRowCount();
// pick 20 cells at random to update
for (let i = 0; i < 20; i++) {
const row = Math.floor(window.agRandom() * rowCount);
const rowNode = gridApi!.getDisplayedRowAtIndex(row)!;
const col = ["a", "b", "c", "d", "e", "f"][i % 6];
rowNode.setDataValue(col, Math.floor(window.agRandom() * 10000));
}
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 25435) % 10000),
b: Math.floor(((i + 323) * 23221) % 10000),
c: Math.floor(((i + 323) * 468276) % 10000),
d: 0,
e: 0,
f: 0,
});
}
return rowData;
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onUpdateSomeValues = onUpdateSomeValues;
}
.align-right {
text-align: right;
}
<div style="height: 100%; display: flex; flex-direction: column">
<div style="margin-bottom: 4px">
<button onclick="onUpdateSomeValues()">Update Some Data</button>
</div>
<div style="flex-grow: 1">
<div id="myGrid" style="height: 100%"></div>
</div>
</div>
In the example above:
- All columns have
enableCellChangeFlash=trueso changes to the cell values will flash the cell. - Clicking Update Some Data will randomly update some data. The grid will then flash the cells where data has changed.
To change the length of the effect, use the grid options cellFlashDuration and cellFadeDuration.
By default, value changes caused by updates to column filters are not highlighted with cell flashing. This behaviour can be toggled by enabling the grid option allowShowChangeAfterFilter.
Flash Cells using the API Copy Link
Alternatively flash cells using the grid API flashCells(params). The params object takes a list of columns and rows to flash, together with the flashDuration and the fadeDuration values.
Flash rows, columns or individual cells. |
When calling flashCells(), pass the flashDuration and fadeDuration values (in milliseconds) as params to change the duration of cell flashing.
import {
CellStyleModule,
ClientSideRowModelModule,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
HighlightChangesModule,
RowApiModule,
CellStyleModule,
ClientSideRowModelModule,
]);
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: [
{ field: "a" },
{ field: "b" },
{ field: "c" },
{ field: "d" },
{ field: "e" },
{ field: "f" },
],
defaultColDef: {
flex: 1,
cellClass: "align-right",
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
};
function onFlashOneCell() {
// pick fourth row at random
const rowNode = gridApi!.getDisplayedRowAtIndex(4)!;
// pick 'c' column
gridApi!.flashCells({ rowNodes: [rowNode], columns: ["c"] });
}
function onFlashTwoColumns() {
// flash whole column, so leave row selection out
gridApi!.flashCells({ columns: ["c", "d"] });
}
function onFlashTwoRows() {
// pick fourth and fifth row at random
const rowNode1 = gridApi!.getDisplayedRowAtIndex(4)!;
const rowNode2 = gridApi!.getDisplayedRowAtIndex(5)!;
// flash whole row, so leave column selection out
gridApi!.flashCells({ rowNodes: [rowNode1, rowNode2] });
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 25435) % 10000),
b: Math.floor(((i + 323) * 23221) % 10000),
c: Math.floor(((i + 323) * 468276) % 10000),
d: 0,
e: 0,
f: 0,
});
}
return rowData;
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onFlashOneCell = onFlashOneCell;
(<any>window).onFlashTwoColumns = onFlashTwoColumns;
(<any>window).onFlashTwoRows = onFlashTwoRows;
}
.align-right {
text-align: right;
}
<div style="height: 100%; display: flex; flex-direction: column">
<div style="margin-bottom: 4px">
<button onclick="onFlashOneCell()" style="margin-left: 15px">Flash One Cell</button>
<button onclick="onFlashTwoRows()">Flash Two Rows</button>
<button onclick="onFlashTwoColumns()">Flash Two Columns</button>
</div>
<div style="flex-grow: 1">
<div id="myGrid" style="height: 100%"></div>
</div>
</div>
In the example above, all three buttons use the flashCells(params) API. Note the following:
- Clicking Flash One Cell uses parameters with one column and one row to flash the intersecting cell.
- Clicking Flash Two Rows uses parameters consisting of two row nodes, causing those rows to flash.
- Clicking Flash Two Columns uses parameters consisting of two columns, causing those columns to flash.
Customise Flash Colour Copy Link
Each time the cell value is changed, the grid adds the CSS class ag-cell-data-changed for 500ms by default, and then the CSS class ag-cell-data-changed-animation for 1,000ms by default. The grid-provided themes use this to apply a background colour. To override the flash background colour, override the relevant CSS class.
import {
CellStyleModule,
ClientSideRowModelModule,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowApiModule,
HighlightChangesModule,
CellStyleModule,
ClientSideRowModelModule,
]);
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: [
{ field: "a" },
{ field: "b" },
{ field: "c" },
{ field: "d" },
{ field: "e" },
{ field: "f" },
],
defaultColDef: {
flex: 1,
cellClass: "align-right",
enableCellChangeFlash: true,
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
};
function onUpdateSomeValues() {
const rowCount = gridApi!.getDisplayedRowCount();
// pick 20 cells at random to update
for (let i = 0; i < 20; i++) {
const row = Math.floor(window.agRandom() * rowCount);
const rowNode = gridApi!.getDisplayedRowAtIndex(row)!;
const col = ["a", "b", "c", "d", "e", "f"][i % 6];
rowNode.setDataValue(col, Math.floor(window.agRandom() * 10000));
}
}
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 25435) % 10000),
b: Math.floor(((i + 323) * 23221) % 10000),
c: Math.floor(((i + 323) * 468276) % 10000),
d: 0,
e: 0,
f: 0,
});
}
return rowData;
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onUpdateSomeValues = onUpdateSomeValues;
}
body {
--ag-value-change-value-highlight-background-color: #cc222244;
}
.align-right {
text-align: right;
}
<div style="height: 100%; display: flex; flex-direction: column">
<div style="margin-bottom: 4px">
<button onclick="onUpdateSomeValues()">Update Some Data</button>
</div>
<div style="flex-grow: 1">
<div id="myGrid" style="height: 100%"></div>
</div>
</div>
The example above demonstrates customising the flashing cell background colour using the --ag-value-change-value-highlight-background-color CSS variable.
Animated Cell Renderers Copy Link
Interesting animations for data changes can be achieved using Cell Components. You can create your own or use one of the provided Show Change Cell Components. The grid provides two such components out of the box.
Animate Show Changed Cells Copy Link
The difference between the previous and new value is temporarily shown beside the new value and is then faded out. This difference is shown in either green or red, for an increase or decrease in value respectively, alongside an arrow indicating the direction of change.
import {
CellStyleModule,
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
TextEditorModule,
ValueParserParams,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
TextEditorModule,
RowApiModule,
CellStyleModule,
ClientSideRowModelModule,
HighlightChangesModule,
]);
const columnDefs: ColDef[] = [
{
headerName: "Editable A",
field: "a",
editable: true,
valueParser: numberValueParser,
},
{
headerName: "Editable B",
field: "b",
editable: true,
valueParser: numberValueParser,
},
{
headerName: "API C",
field: "c",
minWidth: 135,
valueParser: numberValueParser,
cellRenderer: "agAnimateShowChangeCellRenderer",
},
{
headerName: "API D",
field: "d",
minWidth: 135,
valueParser: numberValueParser,
cellRenderer: "agAnimateShowChangeCellRenderer",
},
{
headerName: "Total",
valueGetter: "data.a + data.b + data.c + data.d",
minWidth: 135,
cellRenderer: "agAnimateShowChangeCellRenderer",
},
{
headerName: "Average",
valueGetter: "(data.a + data.b + data.c + data.d) / 4",
minWidth: 135,
cellRenderer: "agAnimateShowChangeCellRenderer",
},
];
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: columnDefs,
defaultColDef: {
minWidth: 105,
flex: 1,
cellClass: "align-right",
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
};
function numberValueParser(params: ValueParserParams) {
return Number(params.newValue);
}
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
function onUpdateSomeValues() {
const rowCount = gridApi!.getDisplayedRowCount();
for (let i = 0; i < 10; i++) {
const row = Math.floor(window.agRandom() * rowCount);
const rowNode = gridApi!.getDisplayedRowAtIndex(row)!;
rowNode.setDataValue("c", Math.floor(window.agRandom() * 10000));
rowNode.setDataValue("d", Math.floor(window.agRandom() * 10000));
}
}
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 25435) % 10000),
b: Math.floor(((i + 323) * 23221) % 10000),
c: Math.floor(((i + 323) * 468276) % 10000),
d: 0,
});
}
return rowData;
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onUpdateSomeValues = onUpdateSomeValues;
}
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
.align-right {
text-align: right;
}
<div class="example-wrapper">
<div style="margin-bottom: 5px">
<button onclick="onUpdateSomeValues()">Update Some C & D Values</button>
</div>
<div id="myGrid"></div>
</div>
The example above demonstrates the following:
- Columns A, B are editable.
- Columns C and D are updated via clicking the button.
- Changes to any of the first 4 columns results in animations in the Total and Average column.
- This can be set as a cell renderer in the column definitions:
const gridOptions = {
columnDefs: [
// set the cell renderer in the column definition
{ cellRenderer: "agAnimateShowChangeCellRenderer" },
],
// other grid options ...
} Animate Slide Cells Copy Link
The previous value is shown in a faded fashion and slides, giving a ghosting effect as the old value fades and slides away.
import {
CellStyleModule,
ClientSideRowModelModule,
ColDef,
GridApi,
GridOptions,
HighlightChangesModule,
ModuleRegistry,
RowApiModule,
TextEditorModule,
ValueParserParams,
createGrid,
enableDevValidations,
} from "ag-grid-community";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
TextEditorModule,
RowApiModule,
CellStyleModule,
ClientSideRowModelModule,
HighlightChangesModule,
]);
const columnDefs: ColDef[] = [
{
headerName: "Editable A",
field: "a",
editable: true,
valueParser: numberValueParser,
},
{
headerName: "Editable B",
field: "b",
editable: true,
valueParser: numberValueParser,
},
{
headerName: "API C",
field: "c",
minWidth: 135,
valueParser: numberValueParser,
cellRenderer: "agAnimateSlideCellRenderer",
},
{
headerName: "API D",
field: "d",
minWidth: 135,
valueParser: numberValueParser,
cellRenderer: "agAnimateSlideCellRenderer",
},
{
headerName: "Total",
valueGetter: "data.a + data.b + data.c + data.d",
minWidth: 135,
cellRenderer: "agAnimateSlideCellRenderer",
},
{
headerName: "Average",
valueGetter: "(data.a + data.b + data.c + data.d) / 4",
minWidth: 135,
cellRenderer: "agAnimateSlideCellRenderer",
},
];
let gridApi: GridApi;
const gridOptions: GridOptions = {
columnDefs: columnDefs,
defaultColDef: {
minWidth: 105,
flex: 1,
cellClass: "align-right",
valueFormatter: (params) => {
return formatNumber(params.value);
},
},
rowData: createRowData(),
};
function numberValueParser(params: ValueParserParams) {
return Number(params.newValue);
}
function formatNumber(number: number) {
return Math.floor(number).toLocaleString();
}
function onUpdateSomeValues() {
const rowCount = gridApi!.getDisplayedRowCount();
for (let i = 0; i < 10; i++) {
const row = Math.floor(window.agRandom() * rowCount);
const rowNode = gridApi!.getDisplayedRowAtIndex(row)!;
rowNode.setDataValue("c", Math.floor(window.agRandom() * 10000));
rowNode.setDataValue("d", Math.floor(window.agRandom() * 10000));
}
}
function createRowData() {
const rowData = [];
for (let i = 0; i < 20; i++) {
rowData.push({
a: Math.floor(((i + 323) * 25435) % 10000),
b: Math.floor(((i + 323) * 23221) % 10000),
c: Math.floor(((i + 323) * 468276) % 10000),
d: 0,
});
}
return rowData;
}
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).onUpdateSomeValues = onUpdateSomeValues;
}
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
.align-right {
text-align: right;
}
<div class="example-wrapper">
<div style="margin-bottom: 5px">
<button onclick="onUpdateSomeValues()">Update Some C & D Values</button>
</div>
<div id="myGrid"></div>
</div>
The example above demonstrates the following:
- Columns A, B are editable.
- Columns C and D are updated via clicking the button.
- Changes to any of the first 4 columns results in animations in the Total and Average column.
- This can be set as a cell renderer in the column definitions:
const gridOptions = {
columnDefs: [
// set the cell renderer in the column definition
{ cellRenderer: "agAnimateSlideCellRenderer" },
],
// other grid options ...
}