Using grid.setDynamicContentHandler, I get the row, but how can I get the column?

I need to know which column was right-clicked to build the grid context menu.

Checking the source, I see the dynamicContentHandler is invoked from GridContextMenu.onBeforeOpenMenu(JsonObject eventDetail)

Setting a breakpoint in that eventDetail looks like this:

{“key”:“3”,“columnId”:“”}

So, column, but not

Related issue: Provide columnId for grid context menu dynamic content handler · Issue #1939 · vaadin/flow-components · GitHub

Thanks.

Registered in 2020, flagged as an enhancment and still open :frowning:

The linked duplicate #424 suggests a workaround; Subclass Grid and use a modified gridConnector.ts. That sounds possible, but not fun.

The linked issue #1939 had a much simpler workaround suggested by @Tatu2:

String expression = "function(){const col=element.getEventContext(event).column;return col ? col.id : '';}()";
getElement().addEventListener("contextmenu", event -> {
    String colId = event .getEventData().getString(expression);
    Optional<Column<Bean>> column = getColumns().stream().filter(col -> colId.equals(col.getId().get())).findFirst();
    System.out.println("setting domevent column id: " + column.get().getId().get());
    domEventColumn = column.get();
 }).addEventData(expression);

It didn’t work out of the box, but if I also give all my columns an id, with column.setId(xx), then it works. Maybe Vaadin set id earlier?

Since we probably want IDs anyway for testing, that is an acceptable solution.