Creating Plugins

Build custom ZenGrid plugins with GridPlugin, GridStore, and GridApi.

Custom plugins implement GridPlugin and are installed with grid.usePlugin(plugin).

Minimal Plugin

row-count-plugin.ts
import type { GridPlugin } from '@zengrid/core';
export function createRowCountPlugin(): GridPlugin {
return {
name: 'rowCountLogger',
phase: 300,
setup(store, api) {
api.register('rowCountLogger', {
log: () => console.log(store.get('rows.viewCount')),
});
return { teardown: [] };
},
};
}

Install The Plugin

install-plugin.ts
import { Grid } from '@zengrid/core';
import { createRowCountPlugin } from './row-count-plugin';
const grid = new Grid(document.getElementById('grid')!, {
rowCount: rows.length,
colCount: columns.length,
rowHeight: 36,
colWidth: columns.map((column) => column.width ?? 140),
columns,
});
grid.usePlugin(createRowCountPlugin());
const log = grid.getGridApi().getMethod('rowCountLogger', 'log');
log?.();

State, Actions, And Events

stateful-plugin.ts
export function createHighlightPlugin(): GridPlugin {
return {
name: 'highlighter',
phase: 300,
setup(store, api) {
store.extend('highlighter.rows', [], 'highlighter');
store.action('highlighter:addRow', (row: number) => {
const rows = store.get('highlighter.rows') as number[];
store.set('highlighter.rows', [...rows, row]);
}, 'highlighter');
api.register('highlighter', {
addRow: (row: number) => {
store.exec('highlighter:addRow', row);
api.fireEvent('highlighter:change', {
rows: store.get('highlighter.rows'),
});
},
getRows: () => store.get('highlighter.rows'),
});
return { teardown: [] };
},
};
}
use-highlighter.ts
const api = grid.getGridApi();
api.getMethod('highlighter', 'addRow')?.(5);
console.log(api.getMethod('highlighter', 'getRows')?.());
Info

Use grid.getGridApi() for plugin APIs. Keep normal application behavior on the public Grid instance when possible.