Searching elements in Tree boxIs 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="https://community.jmp.com/"></SET>
Automation and ScriptingRe: Searching elements in Tree boxIt 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).
Re: Searching elements in Tree boxI 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."

Re: Searching elements in Tree boxDid you look at the examples I provided?
Re: Searching elements in Tree boxThe 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?
Re: Searching elements in Tree boxI 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
Re: Searching elements in Tree boxDim 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?
Re: Searching elements in Tree boxHave 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.
Re: Searching elements in Tree boxI 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 );
Re: Searching elements in Tree boxYou 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
Re: Searching elements in Tree boxThe meaning seems to suggest that even if the Clone Box method is used, it cannot avoid the fact that when a copied tree node is removed, the original tree and its root are also removed.
This means that each time a keyword is changed, a new tree must be established, and then the nodes that match the keyword are removed. As a result, if there are many nodes, searching for keywords becomes very time-consuming in terms of computation.
Are there any alternative methods to create a completely independent copy of the original tree?
Re: Searching elements in Tree boxYou can go over the tree box and create copy "manually" (loop over the tree and build a copy) or rebuild it based on some other data structure (data table / associative array / your own custom class for example). Or you can try if there are methods to prevent this from happening by using expressions / functions.
Re: Searching elements in Tree boxIt seems that I can only regenerate a new tree each time. I changed it to the following JSL, and it works properly when the keyword is 1, but if I enter 2, 4, or something similar, it doesn't succeed. Could you please let me know what the problem might be?
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" );
filter_itemsSensorListTree = Function( {this, searchText},
{filtered_items, i},
// only attempt to filter if there is any text
If( searchText != "",
tree << delete;
search_term = searchText;
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 ) );
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 ),
is_hidden = 0,
cur_child << remove
);
);
If( is_hidden,
tree << Collapse( root ),
tree << Expand( root )
);
);
vlb << append( tree );
,
);
);
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 );
nw = New Window( "TreeBox",
H List Box(
Icon Box( "SearchIndex" ),
Spacer Box( size( 5, 10 ) ),
item_search_teb_AllSensorTree = Text Edit Box( "",
<<SET width=""></SET>
Re: Searching elements in Tree boxThis 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=""></SET>
</CODE></P>
<P> </P></LINGO-BODY><LINGO-SUB id="lingo-sub-869149" slang="en-US" mode="CREATE">Re: Searching elements in Tree box</LINGO-SUB><LINGO-BODY id="lingo-body-869149" slang="en-US" mode="CREATE"><P><SPAN>I tried to change it to the following JSL. I want to get the label name by pressing a button after selecting a node. However, this results in an error when selecting a new option on the tree after entering keywords for filtering, possibly because `new_tree` is just a local variable. How should I fix it so that I can obtain the selected label from the new tree after the keyword search?</SPAN></P>
<PRE><CODE class=" language-jsl">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=""></SET>
Re: Searching elements in Tree boxYour Button Box is referencing wrong display box as the tree it is referencing to has been deleted. There are good ways around this via updating the reference but you can also make it dynamic and always look for the latest tree box inside vlb for example.
Button Box("Select",
label_selected = (vlb[TreeBox(1)] << get selected) << get label;
Try(ShowText << delete);
VLB2 << Append(ShowText = Text Box(label_selected));
),