cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
View Original Published Thread

Searching elements in Tree box

BabyDoragon
Level II

Is it feasible to search for elements in a tree box?
The method shown in the link below searches for user-entered elements in a list box, but this approach does not apply to a tree box.

Searching elements in listbox 

This is because a list box can easily use a loop to search through all items. What methods can be used to search for elements in a tree box?

The following JSL is an example of creating a simple tree box.
dt=New Table( "Food Tree",
	Add Rows( 8 ),
	New Column( "Name",
		Character,
		"Nominal",
		Set Selected,
		Set Values(
			{"Apple", "Banana", "Watermelon", "Fruit", "Broccoli", "Lettuce",
			"Potato", "Vegetable"}
		)
	),
	New Column( "Category",
		Character,
		"Nominal",
		Set Values(
			{"Fruit", "Fruit", "Fruit", "", "Vegetable", "Vegetable", "Vegetable",
			""}
		)
	),
	New Column( "Level",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [2, 2, 2, 1, 2, 2, 2, 1] )
	),
	Set Row States( [0, 0, 0, 1, 0, 0, 0, 0] )
);
relations = [=>];
For Each Row( dt,
	relations[:Name] = Tree Node( :Name );
	relations[:Name] <<Set Data( Eval List( {:Name, :Category,  :Level} ) );
);
roots = {};
For Each Row( dt,
	If( :Category != "" & relations << Contains( :Category ),
		relations[:Category] << Append( relations[:Name] )
	,
		roots[N Items( roots ) + 1] = relations[:Name]
	);
);
New Window( "TreeBox Tests", 
	tree = Tree Box( roots, Size( 300, 500 ) ) );

key = relations << First;
While( !Is Empty( key ),
	Try( tree << Expand( relations[key] ) );
	key = relations << Next( key )
);
tree << Set Node Select Script(
	Function( {this, node},
		Print( node << Get Data )
	)
);



1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User


Re: Searching elements in Tree box

This very quick example might give some ideas

Names Default To Here(1);

initialize_tree = function({}, {Default Local},
	root1 = Tree Node("Parent 1");
	root2 = Tree Node("Parent 2");

	c1 = Tree Node("Child 1");
	c2 = Tree Node("Child 2");
	c3 = Tree Node("Child 11");
	c4 = Tree Node("Child 4");
	root1 << Append(c1);
	root1 << Append(c2);
	root2 << Append(c3);
	root2 << Append(c4);
	tree = Tree Box({root1, root2}, Size(300, 200));
	
	tree << Expand(root1);
	tree << Expand(root2);
	
	return(tree);
);


filter_itemsSensorListTree = Function({vlb, search_term}, {Default Local}, 
	If(search_term != "", 
		new_tree = initialize_tree();
		For Each({root}, new_tree << get roots,
			For(i = root << Get Child Count, i >= 1 , i--,
				cur_child = root << get child(i);
				If(Contains(cur_child << get label, search_term),
					new_tree << Make Visible(cur_child);
				,
					cur_child << remove;
				);
			);
			If(root << Get Child Count == 0,
				new_tree << remove root(root);
			);
		);
	,
		new_tree = initialize_tree();
	);
	vlb_new = V List Box(new_tree);
	vlb << Inval;
	(vlb << Child) << Delete Box();
	vlb << Append(vlb_new);
	vlb << Update Window;
);

nw = New Window( "TreeBox",
	H List Box(
		Icon Box( "SearchIndex" ),
		Spacer Box( size( 5, 10 ) ),
		item_search_teb_AllSensorTree = Text Edit Box("",
			<<set width( 150 ), 
			<<set function(function({this},
				filter_itemsSensorListTree(vlb, this << get text)
			))
		),
		Spacer Box(size( 5, 10 )), 

	),
	vlb = V List Box(
		V List Box(
			initialize_tree()
		)
	);
);

 

-Jarmo

View solution in original post

15 REPLIES 15
jthi
Super User


Re: Searching elements in Tree box

It is feasible, how do wish to search for them? What you wish to do after they are found? 

 

For example take a look old script I mentioned here Directory Tree: Explore Space Used by Folders from my github: jthi-jmp-tools/file-indexer (github). It uses tree box and also has a search box. Other example of the search can be found from JSL Function List add-in (I assume it is in Marketplace nowadays but I don't really use Marketplace it so I'm not sure).

-Jarmo
BabyDoragon
Level II


Re: Searching elements in Tree box

I hope that after entering a keyword, only the items matching the keyword and their parent items will be displayed, as shown in the figure below.
For example, when I enter "water," the tree box will only show "watermelon" and its parent item "Fruit."

 

BabyDoragon_0-1744620167241.png

 

jthi
Super User


Re: Searching elements in Tree box

Did you look at the examples I provided?

-Jarmo
BabyDoragon
Level II


Re: Searching elements in Tree box

The information in GitHub is too complicated... I can't understand the examples, I only know that the clue should be in the function `search_tree`.

Could you please help provide a more straightforward example?

jthi
Super User


Re: Searching elements in Tree box

I don't remember if you were able to hide leaves without removing them, but you can at least dim them

Names Default To Here(1);

root1 = Tree Node("Parent 1");
root2 = Tree Node("Parent 2");

c1 = Tree Node("Child 1");
c2 = Tree Node("Child 2");
c3 = Tree Node("Child 3");
c4 = Tree Node("Child 4");

root1 << Append(c1);
root1 << Append(c2);
root2 << Append(c3);
root2 << Append(c4);

nw = New Window("TreeBox",
	tree = Tree Box({root1, root2}, Size(300, 200))
);
tree << Expand(root1);
tree << Expand(root2);

search_term = " 1";

For Each({root}, tree << get roots,
	is_hidden = 1;
	For(i = 1, i <= root << Get Child Count, i++,
		cur_child = root << get child(i);
		If(Contains(cur_child << get label, search_term),
			tree << make visible(cur_child);
			is_hidden = 0;
		,
			cur_child << Set Dimmed(1);
		);
	);
	If(is_hidden,
		tree << Collapse(root);
	,
		tree << Expand(root);
	);
);

Depending on how complicated tree you have, you might want to look into recursion

-Jarmo
BabyDoragon
Level II


Re: Searching elements in Tree box

Dim is a good way to distinguish whether items meet the search keywords, but if the number of children is large, relying only on dimming their colors can make the search results hard to focus on. For example, when searching for the keyword "1" among the children from 1 to 100, the matching children like 1, 11-19, and 100 cannot be displayed prominently together.
It would be better if we could directly hide the child options compare to dim them.
Is there currently an effective way to directly hide non-matching child items like dim does?
jthi
Super User


Re: Searching elements in Tree box

Have you checked what Scripting Index offers for Tree Box() and Tree Node()? If I remember correctly, you cannot hide nodes in Tree Box() so you have to use other methods of basically re-creating the tree with less nodes. This isn't too difficult to do but it will require extra JSL and some parameter management.

-Jarmo
BabyDoragon
Level II


Re: Searching elements in Tree box

I want to proceed by removing nodes. Each time the keyword changes, I will remove the node again. For example, in JSL, the first keyword is 1, then it changes to 2, but it always ends up with an error. Why is that? According to this logic, how should I correct it?

Names Default To Here(1);

root1 = Tree Node("Parent 1");
root2 = Tree Node("Parent 2");

c1 = Tree Node("Child 1");
c2 = Tree Node("Child 2");
c3 = Tree Node("Child 11");
c4 = Tree Node("Child 4");

root1 << Append(c1);
root1 << Append(c2);
root2 << Append(c3);
root2 << Append(c4);

nw = New Window("TreeBox",
	vlb = V List Box();
	
);
search_term = " 1";
tree = Tree Box({root1, root2}, Size(300, 200));
tree << Expand(root1);
tree << Expand(root2);
treeDuplicate=tree;
For Each({root}, treeDuplicate << get roots,
	is_hidden = 1;
	For(i = 1, i <= root << Get Child Count, i++,
		cur_child = root << get child(i);
		If(Contains(cur_child << get label, search_term),
			is_hidden = 0;
		,
			
			cur_child << remove;
		);
	);
	If(is_hidden,
		treeDuplicate << Collapse(root);
	,
		treeDuplicate << Expand(root);
	);
);

vlb << append(treeDuplicate );
wait(3);

treeDuplicate<< delete;

wait(3);
search_term = " 2";
treeDuplicate=tree;
For Each({root}, treeDuplicate << get roots,
	is_hidden = 1;
	For(i = 1, i <= root << Get Child Count, i++,
		cur_child = root << get child(i);
		If(Contains(cur_child << get label, search_term),
			is_hidden = 0;
		,
			
			cur_child << remove;
		);
	);
	If(is_hidden,
		treeDuplicate << Collapse(root);
	,
		treeDuplicate << Expand(root);
	);
);
vlb << append(treeDuplicate );

 

jthi
Super User


Re: Searching elements in Tree box

You are deleting the tree and you won't have access to it. Instead of duplicate = tree use cloning or other method of copying the display box structure. You have to make sure that all the nodes are also being cloned/copied over correctly and that they do not refer to same objects (copy vs deep copy).

Names Default To Here(1);

root1 = Tree Node("Parent 1");
root2 = Tree Node("Parent 2");

c1 = Tree Node("Child 1");
c2 = Tree Node("Child 2");
c3 = Tree Node("Child 11");
c4 = Tree Node("Child 4");

root1 << Append(c1);
root1 << Append(c2);
root2 << Append(c3);
root2 << Append(c4);

nw = New Window("TreeBox",
	vlb = V List Box();
	
);
tree = Tree Box({root1, root2}, Size(300, 200));
tree << Expand(root1);
tree << Expand(root2);

treeDuplicate = tree;
treeDuplicate1 = tree << clone box;
show(
	treeDuplicate == tree, 
	treeDuplicate1 == tree,
	treeDuplicate << get roots == tree << get roots,
	treeDuplicate1 << get roots == tree << get roots
);
treeDuplicate == tree = 1;
treeDuplicate1 == tree = 0;
treeDuplicate << get roots == tree << get roots = 1;
treeDuplicate1 << get roots == tree << get roots = 1; // be careful here
-Jarmo