Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating ui.table columns only works once #3925

Open
falkoschindler opened this issue Oct 28, 2024 · 2 comments
Open

Updating ui.table columns only works once #3925

falkoschindler opened this issue Oct 28, 2024 · 2 comments
Labels
wontfix This will not be worked on

Comments

@falkoschindler
Copy link
Contributor

Description

As noticed by rafik1200 on Reddit, the following code allows to switch table columns only once:

columns_a = [{'name': 'id', 'label': 'ID A', 'field': 'id'}]
columns_b = [{'name': 'id', 'label': 'ID B', 'field': 'id'}]

def toggle_headers(button_name: str):
    table.columns = columns_a if button_name == 'A' else columns_b

table = ui.table(columns=columns_a, rows=[])
ui.button('A', on_click=lambda: toggle_headers('A'))
ui.button('B', on_click=lambda: toggle_headers('B'))

In contrast, it works when re-creating the list of columns every time:

def toggle_headers(button_name: str):
    table.columns = [{'name': 'id', 'label': f'ID {button_name}', 'field': 'id'}]

table = ui.table(columns=[{'name': 'id', 'label': 'ID A', 'field': 'id'}], rows=[])
ui.button('A', on_click=lambda: toggle_headers('A'))
ui.button('B', on_click=lambda: toggle_headers('B'))
@falkoschindler falkoschindler added the bug Something isn't working label Oct 28, 2024
@falkoschindler
Copy link
Contributor Author

https://www.reddit.com/r/nicegui/comments/1gcqt3m/comment/lu5zip5/

Oh, I think I understand! When passing a list of columns to ui.table, the table uses a reference to the original list. This allows doing things like

columns = [{'name': 'id', 'label': 'ID', 'field': 'id'}]

def change_label():
    columns[0]['label'] = 'X'
    table.update()

table = ui.table(columns=columns, rows=[])
ui.button('Change label', on_click=change_label)

But it also means that by assigning columns_b, the original list columns_a is overwritten. In this a bit unexpected, but when developing the new API for ui.table in version 2.0.0, we somehow couldn't come up with a solution that behaves nicely in all circumstances.

A workaround is to make sure to assign copies instead of the original lists:

columns_a = [{'name': 'id', 'label': 'ID A', 'field': 'id'}]
columns_b = [{'name': 'id', 'label': 'ID B', 'field': 'id'}]

def toggle_headers(button_name: str):
    table.columns = columns_a[:] if button_name == 'A' else columns_b[:]

table = ui.table(columns=columns_a[:], rows=[])
ui.button('A', on_click=lambda: toggle_headers('A'))
ui.button('B', on_click=lambda: toggle_headers('B'))

Can we do anything about it? Or should we simply close the issue?

@falkoschindler falkoschindler added this to the 2.6 milestone Oct 28, 2024
@falkoschindler falkoschindler removed this from the 2.6 milestone Nov 18, 2024
@falkoschindler falkoschindler added wontfix This will not be worked on and removed bug Something isn't working labels Nov 18, 2024
@falkoschindler
Copy link
Contributor Author

Let's close this issue since it's expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant