Question about search in tree - findItem

Hi,

I am using the following code to search items in my tree:

tree.findItem(mySearchTerm, false, true);

This works fine for one node but in my tree I may have occurrences of the same text therefore is there a way to open all nodes which have the same value as ‘mySearchTerm’??

Example:
Employee
---->>>> John Doe
------------->>>> Bob Smith
---->>>> Joe Bloggs
------------->>>> Bob Smith
---->>>> John Doe
------------->>>> Frank Jones

If i typed ‘John Doe’ I would like both ‘John Doe’ nodes to open not just the first instance.

I have found a way around this by using a regex matcher for finding nodes matching a search term and then used focusItem and selectItem for any matches.

Example:

var searchTerm = "Joe Bloggs";
var arr = linksTree.getAllFatItems().split(",");
	      
		for (var i = 0; i < arr.length; i++){
			var text = linksTree.getItemText(arr[i]);
						
			if (new RegExp(searchTerm).test(text)){
				linksTree.focusItem(arr[i]);
				linksTree.selectItem(arr[i]);
				found = true;
			}
			else {
				linksTree.closeItem(arr[i]);
			}
		}
		
		arr = linksTree.getAllLeafs().split(",");
		
		for (var i = 0; i < arr.length; i++){
            var text = linksTree.getItemText(arr[i]);
            
            if (new RegExp(searchTerm).test(text)){
                linksTree.focusItem(arr[i]);
                linksTree.selectItem(arr[i]);
                found = true;
            }
            else {
                linksTree.closeItem(arr[i]);
            }
        }

Hope this helps others…

Hi,

calling getItemText is not the best solution in case of big trees as it caused item rendering. So, if you are calling this method for all branches, the whole tree will be rendered after the first search. findItem item allows to search text without rendering all items.

You may try the following:

var id0 = tree.findItem(mySearchTerm); var idN = true; while(id0 != idN && idN){ idN = tree.findItem(mySearchTerm); }

Thanks Alexandra!