How Do I Remove An Item From a ListView Control? #949
Answered
by
ndonkoHenri
AaronCatolico
asked this question in
Q&A
-
I've looked all over the Flet.dev docs, but couldn't find any solution. I have a ListView control with nested controls such as; Text, PopupMenuButton & PopupMenuItem.
Is there some way to remove a child from the ListView that matches the data value (the name in this case) that was selected? |
Beta Was this translation helpful? Give feedback.
Answered by
ndonkoHenri
Jan 29, 2023
Replies: 1 comment 1 reply
-
Try this. I added some little comments to help you understand. If you still don't, then let me know. from flet import *
def main(page: flet.Page):
page.window_width = 400
page.window_height = 300
page.window_center()
page.bgcolor = '#333333'
page.theme_mode = 'dark'
page.update()
def remove_name(n):
print(f"{n[0]=} | {n[1]=}")
lv1.controls.remove(n[1])
page.update()
lv1 = ListView(expand=True)
list_of_names = ['Adam Smith', 'Billy Jones', 'Chris Wright', 'David Bates', 'Edward Hass', 'Frank Trigg',
'George Hintenbooger', 'Howard Bass']
for name in list_of_names:
# store the button in a variable
my_item = PopupMenuButton(content=Text(f'{name}', color='white', weight='bold', size=16))
my_item.items = [
PopupMenuItem(
content=Row([Text('REMOVE', size=12)]),
data=(name, my_item), ## <-------------------- store the reference to the button in a tuple. You could store it in a dictionary or whatever else you want
on_click=lambda e: remove_name(e.control.data))
]
lv1.controls.append(
my_item
)
page.add(lv1)
flet.app(target=main) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
AaronCatolico
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this. I added some little comments to help you understand. If you still don't, then let me know.