tree_documentation.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 and collapse programmatically', '''
  31. The whole tree or individual nodes can be toggled programmatically using the `expand()` and `collapse()` methods.
  32. ''')
  33. def expand_programmatically():
  34. with ui.row():
  35. ui.button('+ all', on_click=lambda: t.expand())
  36. ui.button('- all', on_click=lambda: t.collapse())
  37. ui.button('+ A', on_click=lambda: t.expand(['A']))
  38. ui.button('- A', on_click=lambda: t.collapse(['A']))
  39. t = ui.tree([
  40. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
  41. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  42. ], label_key='id').expand()
  43. @text_demo('Tree with checkboxes', '''
  44. The tree can be used with checkboxes by setting the "tick-strategy" prop.
  45. ''')
  46. def tree_with_checkboxes():
  47. ui.tree([
  48. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
  49. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  50. ], label_key='id', tick_strategy='leaf', on_tick=lambda e: ui.notify(e.value))