I need to compose and display some kind of popup menu with dynamically configured items. Existing ContextMenu sutes quite good but I can’t figure out how to display it programmatically. The behavior I’m trying to achieve is following:
user clicks on a control button
application examines current form data and collects list of appropriate items
collected items transformed to MenuItem’s and placed into a “popup”
“popup” get displayed to user
Any ideas how to make this using standard components?
Popover would be a better alternative, if ContextMenu doesn’t get the job done, but so far it sounds like we’re talking about a simple menu, for which it ContextMenu should be a good fit.
As far as I know, there is no build in way to open the ContextMenu from server side.
Look at my post where I “convert” a client left click to a right click that opens the GridContextMenu. There I also collect data before the ContextMenu is opened.
You may want to use this on a Button Click Listener, so you can trigger a context menu open from the server side that requires once roundtrip:
// not tested code
button.addClickListener(clickEvent -> {
button.getElement().executeJs("""
$0.dispatchEvent(new MouseEvent('contextmenu', { 'view': window, 'bubbles': true, 'cancelable': true }))
""", button ) /* the client element will be used as $0 */
})
Yes, Popover suites good, but I can’t figure out how to render MenuItems inside it.
I’m trying to achieve dynamic behavior when user clicks a tool button - show him a “popup menu” populated with some context specific items or simply run some business logic an display a result.