I hope to be able to search the contents of the Tree box. In the List box's state as shown in the JSL, it is easy to perform a keyword search. However, it's not as straightforward with the Tree box. Could you please let me know what methods can be used to implement keyword searching for items in the 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", hlistbox(
icon box("SearchIndex"),
spacer box(size(5, 10)),
item_search_teb = text edit box("",
<< set width(300),
// The filter_items function does the work
<< set text changed(filter_items)),
reset_button = button box("",
<< set icon("DebuggerDeleteBreakpoint"),
<< set script(
// Clear the filter and call the callback function
item_search_teb << set text("");
filter_items(item_search_teb, "");
),
<< set tip("Clear filter"),
),
),
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 )
)
);
filter_items = Function( {this, searchText},
{filtered_items, i},
// only attempt to filter if there is any text
If( searchText != "",
// new list for groups that match searchText
filtered_items = {};
// Check if each group matches the given text
For( i = 1, i <= N Items( item_list ), i++,
// Insert to our list if it contains our search text (case insensitive)
If( Contains( Lowercase( item_list[i] ), Lowercase( searchText ) ),
Insert Into( filtered_items, item_list[i] );
)
);
,
// else show all groups
filtered_items = item_list;
);
item_disp_box << Set Items( filtered_items );
); // end filter_items function
item_list = {"Apple", "Banana", "Watermelon", "Broccoli", "Lettuce", "Potato"};
nw = new window("Demonstrate Listbox Search",
hlistbox(
icon box("SearchIndex"),
spacer box(size(5, 10)),
item_search_teb = text edit box("",
<< set width(300),
// The filter_items function does the work
<< set text changed(filter_items)),
reset_button = button box("",
<< set icon("DebuggerDeleteBreakpoint"),
<< set script(
// Clear the filter and call the callback function
item_search_teb << set text("");
filter_items(item_search_teb, "");
),
<< set tip("Clear filter"),
),
),
panelbox("Select a state:",
item_disp_box = listbox(item_list, width(200), nlines(10)),
),
)