zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
9.79k stars 580 forks source link

Button in Row, grid.get_selected_row() sometime get None!!!! #1835

Closed lpnueg4 closed 1 year ago

lpnueg4 commented 1 year ago

Description

from nicegui import ui

grid = ui.aggrid({
    'columnDefs': [
        {'headerName': 'Name', 'field': 'name', 'checkboxSelection': True},
        {'headerName': 'Age', 'field': 'age'},
    ],
    'rowData': [
        {'name': 'Alice', 'age': 18},
        {'name': 'Bob', 'age': 21},
        {'name': 'Carol', 'age': 42},
    ],
    'rowSelection': 'multiple',
}).classes('max-h-40')

async def output_selected_rows():
    rows = await grid.get_selected_rows()
    if rows:
        for row in rows:
            ui.notify(f"{row['name']}, {row['age']}")
    else:
        ui.notify('No rows selected.')

async def output_selected_row():
    row = await grid.get_selected_row()
    if row:
        ui.notify(f"{row['name']}, {row['age']}")
    else:
        ui.notify('No row selected!')

with ui.row(): # if not this line, it's ok.
    ui.button('Output selected rows', on_click=output_selected_rows)
    ui.button('Output selected row', on_click=output_selected_row)

ui.run()
lpnueg4 commented 1 year ago

Snipaste_2023-10-19_15-05-30

falkoschindler commented 1 year ago

@lpnueg4 I tried your code and it looks fine to me... 🤔 Can you, please, describe how to reproduce the problem, what you expect to happen, and what is happening instead? Thanks!

lpnueg4 commented 1 year ago

@falkoschindler My mistake, I found the real reason is that when opening multiple tabs, the first tab is not selected, but the second tab is selected. image

falkoschindler commented 1 year ago

Ok, you might need to use page decorators so that different browser tabs don't share their state. See https://nicegui.io/documentation#auto-index_page for more information. This will ensure that each tab has its own ui.table and only receives notifications for its own button events.

lpnueg4 commented 1 year ago

@falkoschindler THX