Skip to content

Commit

Permalink
allow filtering nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 12, 2024
1 parent 9be04c6 commit e3bedd0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions nicegui/elements/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from typing_extensions import Self

from .. import helpers
from ..element import Element
from ..events import GenericEventArguments, ValueChangeEventArguments, handle_event
from .mixins.filter_element import FilterElement


class Tree(Element):
class Tree(FilterElement):

def __init__(self,
nodes: List[Dict], *,
Expand Down Expand Up @@ -36,7 +36,7 @@ def __init__(self,
:param on_tick: callback which is invoked when a node is ticked or unticked
:param tick_strategy: whether and how to use checkboxes ("leaf", "leaf-filtered" or "strict"; default: ``None``)
"""
super().__init__('q-tree')
super().__init__(tag='q-tree', filter=None)
self._props['nodes'] = nodes
self._props['node-key'] = node_key
self._props['label-key'] = label_key
Expand Down
17 changes: 17 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,20 @@ def test_tick_untick_node_or_nodes(screen: Screen):

screen.click('Untick all')
screen.should_contain('Ticked: []')


def test_filter(screen: Screen):
t = ui.tree([
{'id': 'fruits', 'children': [{'id': 'Apple'}, {'id': 'Banana'}, {'id': 'Cherry'}]},
], label_key='id', tick_strategy='leaf-filtered').expand()
ui.button('Filter', on_click=lambda: t.set_filter('a'))

screen.open('/')
screen.should_contain('Apple')
screen.should_contain('Banana')
screen.should_contain('Cherry')

screen.click('Filter')
screen.should_contain('Apple')
screen.should_contain('Banana')
screen.should_not_contain('Cherry')
12 changes: 12 additions & 0 deletions website/documentation/content/tree_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,16 @@ def tick_programmatically():
ui.button('Untick all', on_click=t.untick)


@doc.demo('Filter nodes', '''
You can filter nodes by setting the `filter` property.
The tree will only show nodes that match the filter.
''')
def filter_nodes():
t = ui.tree([
{'id': 'fruits', 'children': [{'id': 'Apple'}, {'id': 'Banana'}]},
{'id': 'vegetables', 'children': [{'id': 'Potato'}, {'id': 'Tomato'}]},
], label_key='id').expand()
ui.input('filter').bind_value_to(t, 'filter')


doc.reference(ui.tree)

0 comments on commit e3bedd0

Please sign in to comment.