public void Search(string search) { var selection = this.GetSelection(); m_Search = SearchTextParser.Parse(search); base.searchString = search; if (selection != null && selection.Count > 0) { this.SetSelection(selection, TreeViewSelectionOptions.FireSelectionChanged | TreeViewSelectionOptions.RevealAndFrame); } }
public void Search(string search) { m_HasSearch = !string.IsNullOrEmpty(search); var selectedId = -1; var selection = new List <int>(this.GetSelection()); if (selection != null && selection.Count != 0) { selectedId = selection[0]; } selection.Clear(); m_Search = SearchTextParser.Parse(search); // I don't use the TreeViewControl base.searchString API, because I didn't get // it to work to display filtered content sorted. // Therefore I perform the filter myself as seen below, by building a new tree // containing a flat list of filtered items only. //base.searchString = search; if (!m_HasSearch) { m_ActiveTree = m_Tree; } else { // Create a node that contains all items that match the search m_ActiveTree = new TreeViewItem { id = 0, depth = -1, displayName = "Root" }; // Get all items in the tree as a flat list var itemsToProcess = new List <TreeViewItem>(); itemsToProcess.Add(m_Tree); for (var n = 0; n < itemsToProcess.Count; ++n) { var item = itemsToProcess[n]; if (item == null) { continue; } if (item.hasChildren) { itemsToProcess.AddRange(item.children); // process all children too (this makes it kind of recursive) } if (item.id == 0) { continue; // ignore tree root node itself } if (!DoesItemMatchSearch(item, search)) { continue; } // Keep track of the selected item if (selectedId == item.id) { selection = new List <int>(); selection.Add(selectedId); } m_ActiveTree.AddChild(item); } // If the search didn't find any item, make sure to add at least one // invisible item, otherwise the TreeViewControl displays an error. if (!m_ActiveTree.hasChildren) { m_ActiveTree.AddChild(new TreeViewItem { id = m_ActiveTree.id + 1, depth = -1, displayName = "" }); } } SortItemsRecursive(m_ActiveTree, OnSortItem); Reload(); if (selection != null && selection.Count > 0) { this.SetSelection(selection, TreeViewSelectionOptions.FireSelectionChanged | TreeViewSelectionOptions.RevealAndFrame); } }