Write a cell adapter
Besides built-in names ('checkbox', 'badge' and so on), a column's cellRenderer and cellEditor accept a function that creates an adapter. An adapter is an object that manages the DOM of one cell with mount, update and destroy. To use React, Vue or Angular components, use the helpers such as reactCell in Cells from components. These notes are for plain JavaScript or when no helper fits.
An adapter that draws a cell
Give the column a function that creates and returns an adapter, not an adapter object. Each cell needs its own state (the DOM it built, subscriptions), so the grid calls the function once per cell.
function scoreRenderer() {
let box, bar, label;
return {
mount(el, ctx) {
box = document.createElement('div');
box.className = 'mxg-cell-center';
bar = document.createElement('span');
bar.style.cssText = 'display:inline-block;height:8px;background:#2f6fed;border-radius:4px';
label = document.createElement('span');
box.append(bar, label);
el.append(box);
this.update(ctx);
},
update(ctx) {
bar.style.width = `${Number(ctx.value) || 0}px`;
label.textContent = ctx.text;
},
destroy() {
box?.remove();
},
};
}
const columns = [{ id: 'score', title: 'Score', kind: 'number', cellRenderer: scoreRenderer }];mount(el, ctx): build the DOM inside the cell elementel. The return value is ignored.update(ctx): the same cell element received another row or value. Calling it repeatedly must give the same result.destroy(): clean up every DOM node, event listener and timer you created.
ctx has row, rowKey, rowIndex, columnId, value, kind, the text text after the column's formatValue, and the column title columnTitle. Treat ctx as read-only. If a button inside the cell must change a row, keep the api you received when creating the grid in a closed-over variable and call it.
An editor that changes a cell
Give cellEditor a function the same way, and the column also needs editable: true. An editor is created for every edit and never reused.
function memoEditor() {
let input;
return {
mount(el, ctx) {
input = document.createElement('input');
input.value = ctx.startKey ?? String(ctx.value ?? '');
input.addEventListener('keydown', (e) => {
if (e.isComposing) return; // do not commit while composing (IME)
if (e.key === 'Enter') ctx.commit();
if (e.key === 'Escape') ctx.cancel();
});
el.append(input);
},
getValue: () => input.value,
focus() {
input.focus();
input.select();
},
setError(message) {
input.setAttribute('aria-invalid', message ? 'true' : 'false');
input.title = message ?? '';
},
isComposing: () => false,
destroy() {
input.remove();
},
};
}
const columns = [{ id: 'memo', title: 'Memo', editable: true, cellEditor: memoEditor }];| Method | When it is called |
|---|---|
mount | When editing starts. ctx.trigger is 'doubleClick', 'key' or 'api', and startKey is the key that opened it |
focus | Right after mount |
getValue | On commit. Validation follows the column's validation |
setError | With a message when validation fails, with null when it is fixed |
isComposing | (optional) Return true while composing text (for example Korean). A ctx.commit() during that time is ignored |
destroy | On commit, cancel and when the grid is destroyed. Clean up everything you built |
ctx.commit() and ctx.cancel() request a commit or cancel; the grid decides whether the value passes validation. Key presses inside an input in the editor are not taken over by the grid's keyboard navigation (input, textarea, select, contenteditable). For Korean input, also see "Type Korean text" in Editing cells.
Set the editor size and placement with the column option editorBox (editing guide).
When adapters are created and cleaned up
The grid draws only the visible rows and reuses the same row elements for other rows as you scroll. So a renderer adapter is created once per cell element, and when the row changes only update is called.
| When | What is called |
|---|---|
| The cell is filled for the first time | your function, then mount |
| Another row comes in through scrolling or a data change | update (the adapter stays) |
| The cell is filled again with the same row | update (the row object may have changed) |
| The column layout changes and row elements are rebuilt; grid destroyed | destroy |
The column's cellRenderer becomes a different function | destroy on the old adapter, then mount on a new one |
An adapter in a cell that scrolled out of view is kept, and update is called when it is filled again. The number of live adapters is about rows drawn on screen × adapter columns. An adapter must not leak state from the previous row into the next one. If it holds state, reset it in update when ctx.rowKey changes.
Vertical centering
el is a plain block element, so text sits in the middle thanks to the row height, but bars, chips, icons and SVG stick to the top of the cell. To draw those, create a div with the class mxg-cell-center inside el and draw into it (as in the example above). A class put on the cell element itself is overwritten when the grid refills it.
When something throws
If mount, update or destroy throws, only that cell is emptied and shows ⚠ Error with the mxg-cell-error class. Other cells and scrolling keep working, an error event (code: 'callback-error', with the column id in where) arrives once per update cycle, and the error is also logged to the console. The next fill tries again with a new adapter.
Security
Put cell text in with textContent. If your adapter inserts HTML strings with innerHTML, the responsibility is the adapter's. ctx.value and ctx.text can be untrusted data.