tree_documentation.py 2.2 KB

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