tree_documentation.py 2.9 KB

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