How to combine ui.context_menu() with aggrid tables? #4695
-
First Check
Example Codefrom nicegui import ui
grid = ui.aggrid({
'columnDefs': [
{'headerName': 'Name', 'field': 'name'},
{'headerName': 'Age', 'field': 'age'},
],
'rowData': [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
],
})
# Show col and cell value
def cmenu(e):
ui.notify(f'{e.args['colId']}: {e.args['value']}')
# Testing with context menu - DOES NOT UPDATE
with ui.context_menu():
ui.menu_item(f'{e.args['colId']}')
ui.menu_item(f'{e.args['value']}')
# Testing with normal menu - WORKS
with ui.menu() as menu:
ui.menu_item(f'{e.args['colId']}')
ui.menu_item(f'{e.args['value']}')
menu.open()
# Attach the context menu event
grid.on('cellContextMenu', lambda e: cmenu(e))
ui.run() DescriptionHi All, I am trying to figure out if we can use But the context menu is always stuck at the first cell that I ever click. It never updates the values on next right clicks. Thanks a lot for the help, NiceGUI Version2.14.1 Python VersionPython 3.13.0 BrowserChrome Operating SystemWindows Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thank you for the interesting scenario @Anindya088 TL-DR: You do NOT instantiate another Rather, you must:
I have also applied type like this: from nicegui import ui
from nicegui.events import GenericEventArguments
grid = ui.aggrid({
'columnDefs': [
{'headerName': 'Name', 'field': 'name'},
{'headerName': 'Age', 'field': 'age'},
],
'rowData': [
{'name': 'Alice', 'age': 30},
{'name': 'Bob', 'age': 25},
],
})
with ui.context_menu() as cmenu: # keep a reference
pass
# Show col and cell value
def populate_cmenu(e: GenericEventArguments):
ui.notify(f'{e.args['colId']}: {e.args['value']}')
cmenu.clear() # clear stuff out
with cmenu: # put stuff back in
ui.menu_item(f'{e.args['colId']}')
ui.menu_item(f'{e.args['value']}')
# Attach the context menu event
grid.on('cellContextMenu', lambda e: populate_cmenu(e))
ui.run() ![]() |
Beta Was this translation helpful? Give feedback.
Thank you for the interesting scenario @Anindya088
TL-DR: You do NOT instantiate another
ui.context_menu()
when you want to change what's in the context menu.Rather, you must:
cmenu
in my code),cmenu.clear()
),with cmenu:
then blahblahblah...)I have also applied type like this:
e: GenericEventArguments
, just soargs
shows up blue, not white. It's a personal preference thing. Get rid of it if you don't like it.