1
0

main.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python3
  2. import time
  3. from nicegui import ui
  4. fields = [
  5. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True},
  6. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  7. ]
  8. rows = [
  9. {'id': 0, 'name': 'Alice', 'age': 18},
  10. {'id': 1, 'name': 'Bob', 'age': 21},
  11. {'id': 2, 'name': 'Lionel', 'age': 19},
  12. {'id': 3, 'name': 'Michael', 'age': 32},
  13. {'id': 4, 'name': 'Julie', 'age': 12},
  14. {'id': 5, 'name': 'Livia', 'age': 25},
  15. {'id': 6, 'name': 'Carol'},
  16. ]
  17. def add_row(item):
  18. rows.append(item)
  19. table.update()
  20. def remove_row(keys):
  21. for i in range(len(rows)):
  22. if rows[i]['id'] in keys:
  23. del rows[i]
  24. break
  25. table.update()
  26. with ui.qtable(title='QTable', columns=fields, rows=rows, key='id', selection='single', pagination=15) as table:
  27. with table.add_slot('top-right'):
  28. with ui.input(placeholder='Search').props('type="search"').bind_value(table, 'filter') as search:
  29. with search.add_slot('append'):
  30. ui.icon('search')
  31. with table.add_slot('top-row'):
  32. with table.row():
  33. with table.cell().props('colspan="100%"'):
  34. ui.label('This is a top row').classes('text-center')
  35. with table.add_slot('bottom-row'):
  36. with table.row():
  37. with table.cell().props('colspan="2"'):
  38. new_name = ui.input()
  39. with table.cell():
  40. ui.button('add row', on_click=lambda: add_row({'id': time.time(), 'name': new_name.value, 'age': 10}))
  41. ui.label('').bind_text_from(table, 'selected', lambda val: f'Current selection: {val.__repr__()}')
  42. ui.button('Remove selection', on_click=lambda: remove_row(table.selected['keys'])).bind_visibility(table, 'selected')
  43. ui.run()