Use grid column information to generate context-menu

Hi there,

I am using Vaadin 14 to develop an application. I use this version as it is the latest LTS version of Vaadin and as such this is a requirement for my current project. I created a grid in which users need to be able to get context menu’s for individual cells in certain columns of the grid. As such, to generate these context menu’s and perform the appropriate actions when their buttons are clicked, I need both information about the item (row) and column.

Up until this point, I have tried the following approaches:

  1. Use addOpenedChangeListener which by default contains this information. Unfortunately, it appears this doesn’t execute before the context menu is opened but rather after it has opened. Changes it makes are only reflected the next time I open a context menu.

  2. Use setDynamicContentHandler which performs the expected behavior, namely executing before the context menu is opened, but has no contextual info about the column.

  3. Use setDynamicContentHandler with an additional addItemClickListener to keep track of the last clicked column. The ItemClickListener would hopefully execute before the DynamicContentHandler and provide it with the necessary column information. Unfortunately, it seems addItemClickListener is not triggered with right clicking. This is weird, as it supports event.getButton(), but anyway.

As such, I am currently rather lost in how I can achieve my goal. Any ideas?

This is still an issue in the current version. I asked the same question last month, and got the following workaround:

OT: Vaadin 14 free support is end of life.

The release model was simplified (more information A simpler release model | Vaadin) - so there is no such thing as “LTS” label - like with Java.

You should use the version 24 if you don’t have technical requirements. Vaadin 24 will be supported for free for a longer time than Vaadin 14.

Same for the paid support.

We have a commercial license, so we should have an additional 10 years of support with Vaadin 14. I just read the article stating that major releases have up to 15 years of maintenance with extended warranty. I’ll discuss this within the team, thanks for the heads up.

However, will going to Vaadin 24 resolve my issue from above?

No, it’s still an open issue.

Thanks, this worked with some minor adjustments (e.g. adding ids to columns & making sure getElement() was executed on my Grid element and not something else)

If you don’t hit a breakpoint in the event listener, I wonder if you’ve added it the right place.
I see the example says:

getElement().addEventListener("contextmenu", ...

Please note that as written, this needs to be in a subclass of Grid.
If you’re not, you need to add your grid instance a couple of places

grid.getElement().addEventListener("contextmenu, ...
    ...
    var vaadinColumn = grid.getColumns()...

Other than that I have no suggestions