Skip to main content

Format data using `rendertext` event

NB: This example is broken: https://github.com/TonyGermaneri/canvas-datagrid/issues/262

Attach to the rendertext event.

import canvasDatagrid from 'canvas-datagrid';
import data from '/data.json';

const app = document.getElementById('app');
const gridElement = document.createElement('div');
const grid = canvasDatagrid({
  parentNode: gridElement,
  data,
  schema: [
    { name: 'col1' },
    {
      name: 'col2',
      type: 'date',
    },
    { name: 'col3' },
  ],
});

grid.addEventListener('rendertext', function (e) {
  if (e.cell.rowIndex > -1) {
    if (e.cell.header.name === 'col2') {
      e.cell.formattedValue = new Date(e.cell.value).toISOString();
    }
  }
});

app.append(gridElement);