How to refresh/update/redraw table with changed values #1307
-
QuestionIf I have a table like this from nicegui import ui
var_age = 23
columns = [
{'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
{'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
]
rows = [
{'name': 'Alice', 'age': 18},
{'name': 'Bob', 'age': 21},
{'name': 'Carol', 'age' : var_age},
]
ui.table(columns=columns, rows=rows, row_key='name')
ui.run() It will print a table with Carol - 23 But what if you var_age changes. How do I refresh/update/redraw the table so it shows the updated value? |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Aug 1, 2023
Replies: 1 comment 1 reply
-
Hi @HerzMichi, var_age = 23
columns = [
{'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
{'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
]
rows = [
{'name': 'Alice', 'age': 18},
{'name': 'Bob', 'age': 21},
{'name': 'Carol', 'age': var_age},
]
table = ui.table(columns=columns, rows=rows, row_key='name')
def handle_click():
global var_age
var_age += 1
rows[2]['age'] = var_age
table.update()
ui.button('Click me', on_click=handle_click) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
HerzMichi
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @HerzMichi,
The table has no reference to
var_age
. So if its value changes, the cell value will still be 23.But if you update the
rows
, you only need to calltable.update
to send the new data to the browser: