tree_documentation.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.tree)
  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. @doc.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. @doc.demo('Expand and collapse programmatically', '''
  31. The whole tree or individual nodes can be toggled programmatically using the `expand()` and `collapse()` methods.
  32. This even works if a node is disabled (e.g. not clickable by the user).
  33. ''')
  34. def expand_programmatically():
  35. t = ui.tree([
  36. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}], 'disabled': True},
  37. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  38. ], label_key='id').expand()
  39. with ui.row():
  40. ui.button('+ all', on_click=t.expand)
  41. ui.button('- all', on_click=t.collapse)
  42. ui.button('+ A', on_click=lambda: t.expand(['A']))
  43. ui.button('- A', on_click=lambda: t.collapse(['A']))
  44. @doc.demo('Tree with checkboxes', '''
  45. The tree can be used with checkboxes by setting the "tick-strategy" prop.
  46. ''')
  47. def tree_with_checkboxes():
  48. ui.tree([
  49. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
  50. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  51. ], label_key='id', tick_strategy='leaf', on_tick=lambda e: ui.notify(e.value))
  52. doc.reference(ui.tree)