- Data Grid docs
- Columns and formatting
Cell renderers
Draw a column's cells with your own text or DOM nodes, such as links, badges or icons, while search, sort and export keep using the data.
A renderer draws a column's cells your way: an order number as a link to the order, a status as a coloured badge, an e-mail address as a mailto: link. It changes what the cell shows, not the data, so search, filters, sorting and exports still work on the values.
Use it
renderers maps a field name to a function. The grid calls it for each cell of that column it draws, with { id, field, text }: the record's ID, the field and the cell's display text. Return a string or a DOM node.
JavaScript
import { mountTable } from '@8sheets/data-grid';
const badge = (text) => {
const span = document.createElement('span');
span.textContent = text;
span.className = `badge badge-${text.toLowerCase()}`;
return span;
};
const grid = await mountTable(document.querySelector('#orders'), {
title: 'Orders',
data: orders,
renderers: {
Order: ({ id, text }) => {
const a = document.createElement('a');
a.href = `/orders/${encodeURIComponent(id)}`;
a.textContent = text;
return a;
},
Status: ({ text }) => badge(text),
Email: ({ text }) => Object.assign(document.createElement('a'), { href: `mailto:${text}`, textContent: text }),
},
});React
import { useMemo } from 'react';
import { DataGrid } from '@8sheets/data-grid-react';
// Renderers return DOM nodes or text, not React elements: the grid draws its cells itself.
function link(href, text) {
const a = document.createElement('a');
a.href = href;
a.textContent = text;
return a;
}
export function Orders({ orders }) {
const options = useMemo(() => ({
title: 'Orders',
data: orders,
renderers: {
Order: ({ id, text }) => link(`/orders/${encodeURIComponent(id)}`, text),
Status: ({ text }) => (text === 'Overdue' ? `⚠ ${text}` : text),
},
}), [orders]);
return <div style={{ height: 480 }}><DataGrid options={options} /></div>;
}Vue
<script setup>
import { computed } from 'vue';
import { DataGrid } from '@8sheets/data-grid-vue';
const props = defineProps({ orders: Array });
// Renderers return DOM nodes or text, not Vue components: the grid draws its cells itself.
function link(href, text) {
const a = document.createElement('a');
a.href = href;
a.textContent = text;
return a;
}
const options = computed(() => ({
title: 'Orders',
data: props.orders,
renderers: {
Order: ({ id, text }) => link(`/orders/${encodeURIComponent(id)}`, text),
Status: ({ text }) => (text === 'Overdue' ? `⚠ ${text}` : text),
},
}));
</script>
<template>
<div style="height: 480px"><DataGrid :options="options" /></div>
</template>Angular
import { Component, Input } from '@angular/core';
import { DataGridComponent, DataGridOptions } from '@8sheets/data-grid-angular';
function link(href: string, text: string) {
const a = document.createElement('a');
a.href = href;
a.textContent = text;
return a;
}
@Component({
selector: 'app-orders',
imports: [DataGridComponent],
template: `<eight-data-grid [options]="options" style="height: 480px" />`,
})
export class Orders {
options: DataGridOptions = {};
@Input() set orders(data: Record<string, unknown>[]) {
this.options = {
title: 'Orders',
data,
renderers: {
Order: ({ id, text }) => link(`/orders/${encodeURIComponent(String(id))}`, text),
Status: ({ text }) => (text === 'Overdue' ? `⚠ ${text}` : text),
},
};
}
}Set text with textContent rather than innerHTML: cell text comes from your data, and from files the user opens.
Limits
- A renderer gets the display text, not the raw value. For a formatted number, set the field's
formatinstead; for the raw value, look the record up byid. - Renderers run each time cells are drawn, while scrolling too. Keep them quick and free of side effects; the grid may call one again for the same cell.
- Exports, copies and the record dialog show the data, not what a renderer drew.
- For a whole panel per record, use master/detail instead.