1
0

tree_documentation.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from typing import List
  2. from nicegui import ui
  3. from ..documentation_tools import text_demo
  4. def main_demo() -> None:
  5. ui.tree([
  6. {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
  7. {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
  8. ], label_key='id', on_select=lambda e: ui.notify(e.value))
  9. def more() -> None:
  10. @text_demo('Tree with custom header and body', '''
  11. Scoped slots can be used to insert custom content into the header and body of a tree node.
  12. See the [Quasar documentation](https://quasar.dev/vue-components/tree#customize-content) for more information.
  13. ''')
  14. def tree_with_custom_header_and_body():
  15. tree = ui.tree([
  16. {'id': 'numbers', 'description': 'Just some numbers', 'children': [
  17. {'id': '1', 'description': 'The first number'},
  18. {'id': '2', 'description': 'The second number'},
  19. ]},
  20. {'id': 'letters', 'description': 'Some latin letters', 'children': [
  21. {'id': 'A', 'description': 'The first letter'},
  22. {'id': 'B', 'description': 'The second letter'},
  23. ]},
  24. ], label_key='id', on_select=lambda e: ui.notify(e.value))
  25. tree.add_slot('default-header', '''
  26. <span :props="props">Node <strong>{{ props.node.id }}</strong></span>
  27. ''')
  28. tree.add_slot('default-body', '''
  29. <span :props="props">Description: "{{ props.node.description }}"</span>
  30. ''')
  31. @text_demo('Expand programmatically', '''
  32. The tree can be expanded programmatically by modifying the `expanded` prop.
  33. ''')
  34. def expand_programmatically():
  35. def expand(node_ids: List[str]) -> None:
  36. t._props['expanded'] = node_ids
  37. t.update()
  38. with ui.row():
  39. ui.button('all', on_click=lambda: expand(['A', 'B']))
  40. ui.button('A', on_click=lambda: expand(['A']))
  41. ui.button('B', on_click=lambda: expand(['B']))
  42. ui.button('close', on_click=lambda: expand([]))
  43. t = ui.tree([
  44. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
  45. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  46. ], label_key='id')