Skip to main content

Context menu using a function for items

Instead of using an array, you can use a function that returns an array.

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,
});

grid.addEventListener('contextmenu', function (e) {
  e.items.push({
    title: 'Function based child context menu item',
    items: function () {
      return [
        {
          title: 'I was added via a function',
          click: function () {
            return;
          },
        },
      ];
    },
  });
});

app.append(gridElement);