Custom renderers
You can also write a custom renderer. To do so, define:
A class for the specific Cell Content (it should extend the abstract
CellContentorCellContentProclass). It must implement lifecycle methods:add,update,delete.class CustomCellContent extends CellContentPro {constructor(cell: TableCell, renderer: CustomCellRenderer) {super(cell, renderer);this.add();}protected override add(): void {// create your content here}public override update(): void {// update your content here, when the cell value has changed}public override delete(): void {// remove the element from DOM, clear event listeners, etc.}}A class representing your Renderer, which inherits from the abstract
CellRendererclass. It should implement:options– an abstract property holding the renderer’s unique configuration optionsrender– a method that creates and returns a new instance of CellContent
export interface CustomRendererOptions extends CellRenderer.Options {type: 'customRenderer';additionalOptions: unknown;}class CustomRenderer extends CellRenderer {public options: CustomRendererOptions;constructor(column: Column, options: CustomRendererOptions) {super(column);this.options = options;}public render(cell: TableCell): CustomCellContent {return new CustomCellContent(cell, this);}}Add the renderer type to
CellRendererRegistryso it can be used in Grid Options.declare module 'highcharts/grid/es-modules/Grid/Pro/CellRendering/CellRendererType' {interface CellRendererTypeRegistry {customRenderer: typeof CustomRenderer;}}CellRendererRegistry.registerRenderer('customRenderer', CustomRenderer);
If you want your custom renderer to be usable in cell edit mode, you need to implement additionally the following interfaces:
EditModeContent- it should extend the custom cell content class.EditModeRenderer- it should extend the custom cell renderer class.
Custom Textarea Renderer
This section demonstrates how to create a custom Textarea cell renderer for the Grid. The custom renderer will display a <textarea> element inside the cell, allowing for multi-line text editing.
- We start by importing the default
CellRendererandCellContentProclasses andCellRendererRegistryfrom theGridnamespace.
const {CellRenderer,CellContentPro,CellRendererRegistry} = Grid;class TextareaContent extends CellContentPro {}
- The next step is to create a new class, such as TextareaContent, which extends the imported
CellContentProclass and is responsible for creating and managing the<textarea>element.
class TextareaContent extends CellContentPro {constructor(cell, renderer) {super(cell, renderer);this.add();}// Required by the interfaceadd(parentElement = this.cell.htmlElement) {const textarea = this.textarea = document.createElement('textarea');this.update();parentElement.appendChild(textarea);return textarea;}// Required by the interfaceupdate() {this.textarea.value = this.cell.value;}// Required by the interfacedestroy() {this.textarea.remove();}}
- The TextareaRenderer class is responsible for integrating the custom textarea content into the grid. By extending the
CellRendererbase class, it provides arendermethod that creates and returns a new instance ofTextareaContentfor each cell.
class TextareaRenderer extends CellRenderer {constructor(column, options) {super(column);this.options = options;}render(cell) {return new TextareaContent(cell, this);}}
- Register the new renderer type with the
CellRendererRegistryso it can be used in the Grid configuration.
CellRendererRegistry.registerRenderer('textarea', TextareaRenderer);
Once registered, you can use the custom textarea renderer in your column configuration:
columns: [{id: 'description',cells: {renderer: {type: 'textarea'}}}]
See also Editing / Renderers.