Filter the Nodes

Even though the TreeView does not provide a built-in filtering functionality, the component delivers a generic approach for filtering hierarchical data recursively.

The following example demonstrates a sample search function that iterates over an array of hierarchical data and produces a new one based on a filter value and a predefined predicate.

public search(items: any[], term: string): any[] {
    return items.reduce((acc, item) => {
        if (this.contains(item.text, term)) {
            acc.push(item);
        } else if (item.items && item.items.length > 0) {
            let newItems = this.search(item.items, term);

            if (newItems.length > 0) {
            acc.push({ text: item.text, items: newItems });
            }
        }

        return acc;
    }, []);
}

public contains(text: string, term: string): boolean {
    return text.toLowerCase().indexOf(term.toLowerCase()) >= 0;
}
Example
View Source
Edit In Stackblitz  
Change Theme:

In this article

Not finding the help you need?