Search code examples
vaadinvaadin-flowvaadin-grid

Open a GridContextMenu on a button click


We have a column with a collapsed overflow MenuBar on a grid with plenty of items and the rendering performance was really terrible, even using lazy loading.

We replaced it with a GridContextMenu and the grid is loading much faster now. But the users will miss the menu bar button to invoke the menu by clicking on it. I'm trying to find a way to trigger the opening of Context Menu from a native button render column.

Is there a way to open the context menu programatically or to emulate the right click on a grid row?

I've tried to implement the menubar with a LitRenderer but ContextMenu seemed a lot easier and maintainable. The only missing part is to be able to invoke it by clicking on some element in a column.

Something like this:

grid.addColumn(new NativeButtonRenderer<>(
        item -> "..." ,
        clickedItem -> {
            openContextMenu(item) // how to implement openContextMenu?
        })
);

Solution

  • Say that your ContextMenu is menu and you have a menuButton of type Button, this is, how it is done:

    menu.setTarget(menuButton);
    menu.setOpenOnClick(true);