How to access buttons created via table slot? #3875
-
QuestionHello, In the below code, I created a table with row buttons following the example in #815. I want to be able to enable/disable the buttons programmatically, but I'm not sure how to access them since they were created through the table's body-cell-[name] scoped slot. I would appreciate any help, and thank you in advance! from nicegui import ui
table = ui.table(
columns=[
{
"name": "name",
"label": "Name",
"field": "name",
"required": True,
"align": "left",
},
{
"name": "age",
"label": "Age",
"field": "age",
"required": True,
"align": "left",
},
{
"name": "buttons",
"label": "",
"field": "buttons",
"align": "center",
},
],
rows=[
{"id": "0", "name": "Bob", "age": 34},
{"id": "1", "name": "Jill", "age": 35},
],
row_key="id",
title="Entries",
selection=None,
pagination=0,
on_select=None,
on_pagination_change=None,
)
table.add_slot(
name="body-cell-buttons",
template=r"""
<q-td :props="props" colspan="100%">
<q-btn icon="edit" flat dense />
<q-btn icon="people" flat dense />
</q-td>
""",
)
ui.run() |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Oct 17, 2024
Replies: 2 comments 3 replies
-
from nicegui import ui
table = ui.table(
columns=[
{
"name": "name",
"label": "Name",
"field": "name",
"required": True,
"align": "left",
},
{
"name": "age",
"label": "Age",
"field": "age",
"required": True,
"align": "left",
},
{
"name": "buttons",
"label": "",
"field": "buttons",
"align": "center",
},
],
rows=[
{"id": "0", "name": "Bob", "age": 34},
{"id": "1", "name": "Jill", "age": 35},
],
row_key="id",
title="Entries",
selection=None,
pagination=0,
on_select=None,
on_pagination_change=None,
)
switch = ui.switch('show or hide',value=False)
with table.add_slot("body-cell-buttons"),ui.element('q-td').props('colspan="100%'):
ui.button(icon='edit').props('flat dense').bind_visibility_from(switch,'value')
ui.button(icon='people').props('flat dense').bind_visibility_from(switch,'value')
switch.move()
ui.run() |
Beta Was this translation helpful? Give feedback.
2 replies
-
You can add an "enabled" field to the row data and use it in your slot template: table = ui.table(
columns=[
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
{'name': 'buttons', 'label': '', 'field': 'buttons'},
],
rows=[
{'id': '0', 'name': 'Bob', 'age': 34, 'enabled': True},
{'id': '1', 'name': 'Jill', 'age': 35, 'enabled': True},
],
)
table.add_slot(name='body-cell-buttons', template=r'''
<q-td :props="props" colspan="100%">
<q-btn icon="edit" flat dense :disable="!props.row.enabled" />
<q-btn icon="people" flat dense :disable="!props.row.enabled" />
</q-td>
''')
def enable(value: bool):
for row in table.rows:
row['enabled'] = value
table.update()
ui.button('Disable', on_click=lambda: enable(False))
ui.button('Enable', on_click=lambda: enable(True)) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
vahuynh12
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can add an "enabled" field to the row data and use it in your slot template: