tree_documentation.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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('Tree with checkboxes', '''
  31. The tree can be used with checkboxes by setting the "tick-strategy" prop.
  32. ''')
  33. def tree_with_checkboxes():
  34. ui.tree([
  35. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}]},
  36. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  37. ], label_key='id', tick_strategy='leaf', on_tick=lambda e: ui.notify(e.value))
  38. @doc.demo('Expand/collapse programmatically', '''
  39. The whole tree or individual nodes can be toggled programmatically using the `expand()` and `collapse()` methods.
  40. This even works if a node is disabled (e.g. not clickable by the user).
  41. ''')
  42. def expand_programmatically():
  43. t = ui.tree([
  44. {'id': 'A', 'children': [{'id': 'A1'}, {'id': 'A2'}], 'disabled': True},
  45. {'id': 'B', 'children': [{'id': 'B1'}, {'id': 'B2'}]},
  46. ], label_key='id').expand()
  47. with ui.row():
  48. ui.button('+ all', on_click=t.expand)
  49. ui.button('- all', on_click=t.collapse)
  50. ui.button('+ A', on_click=lambda: t.expand(['A']))
  51. ui.button('- A', on_click=lambda: t.collapse(['A']))
  52. @doc.demo('Select/deselect programmatically', '''
  53. You can select or deselect nodes with the `select()` and `deselect()` methods.
  54. ''')
  55. def select_programmatically():
  56. t = ui.tree([
  57. {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
  58. {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
  59. ], label_key='id').expand()
  60. with ui.row():
  61. ui.button('Select A', on_click=lambda: t.select('A'))
  62. ui.button('Deselect A', on_click=t.deselect)
  63. @doc.demo('Tick/untick programmatically', '''
  64. After setting a `tick_strategy`, you can tick or untick nodes with the `tick()` and `untick()` methods.
  65. You can either specify a list of node keys or `None` to tick or untick all nodes.
  66. ''')
  67. def tick_programmatically():
  68. t = ui.tree([
  69. {'id': 'numbers', 'children': [{'id': '1'}, {'id': '2'}]},
  70. {'id': 'letters', 'children': [{'id': 'A'}, {'id': 'B'}]},
  71. ], label_key='id', tick_strategy='leaf').expand()
  72. with ui.row():
  73. ui.button('Tick 1, 2 and B', on_click=lambda: t.tick(['1', '2', 'B']))
  74. ui.button('Untick 2 and B', on_click=lambda: t.untick(['2', 'B']))
  75. with ui.row():
  76. ui.button('Tick all', on_click=t.tick)
  77. ui.button('Untick all', on_click=t.untick)
  78. @doc.demo('Filter nodes', '''
  79. You can filter nodes by setting the `filter` property.
  80. The tree will only show nodes that match the filter.
  81. ''')
  82. def filter_nodes():
  83. t = ui.tree([
  84. {'id': 'fruits', 'children': [{'id': 'Apple'}, {'id': 'Banana'}]},
  85. {'id': 'vegetables', 'children': [{'id': 'Potato'}, {'id': 'Tomato'}]},
  86. ], label_key='id').expand()
  87. ui.input('filter').bind_value_to(t, 'filter')
  88. doc.reference(ui.tree)