在树框中搜索元素在树形框中搜索元素是否可行?
以下链接中显示的方法在列表框中搜索用户输入的元素,但这种方法不适用于树框。
在列表框中搜索元素
这是因为列表框可以轻松地使用循环来搜索所有项目。可以使用什么方法在树形框中搜索元素?
以下 JSL 是创建简单树框的示例。
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>
<P><SPAN><BR /><BR /></SPAN></P><LINGO-LABS id="lingo-labs-867118" slang="en-US" mode="CREATE"><LINGO-LABEL>自动化和脚本</LINGO-LABEL></LINGO-LABS>回复:在树框中搜索元素可以,你想怎么找到它们?找到之后你想做什么?
例如,看看我之前提到的旧脚本“目录树:探索文件夹使用的空间”,它来自我的 GitHub: jthi-jmp-tools/文件索引器 (github) 。它使用树形框,并且带有一个搜索框。其他搜索示例可以在JSL 函数列表插件中找到(我猜它现在在 Marketplace 里,但我很少使用它,所以不太确定)。
回复:在树框中搜索元素我希望输入一个关键字之后,只显示符合该关键字的商品以及它们的父商品,如下图所示。
例如,当我输入“水”时,树框将仅显示“西瓜”及其父项“水果”。

回复:在树框中搜索元素你看过我提供的例子吗?
回复:在树框中搜索元素GitHub 上的信息太复杂了...我看不懂这些例子,只知道线索应该在函数 `search_tree` 中。
您能否帮忙提供一个更直接的例子?
回复:在树框中搜索元素我不记得你是否可以在不移除树叶的情况下隐藏它们,但你至少可以将它们调暗
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);
);
);
根据树的复杂程度,你可能需要研究递归
回复:在树框中搜索元素调暗颜色是区分商品是否符合搜索关键词的好方法,但如果子商品数量较多,仅依靠调暗颜色会使搜索结果难以集中。例如,在 1 到 100 的子商品中搜索关键词“1”时,匹配的子商品(例如 1、11-19 和 100)无法一起突出显示。
如果我们可以直接隐藏子选项而不是使它们变暗,那就更好了。
目前是否有一种有效的方法可以像 dim 那样直接隐藏不匹配的子项?
回复:在树框中搜索元素你检查过 Scripting Index 为 Tree Box() 和 Tree Node() 提供了什么功能吗?如果我没记错的话,Tree Box() 中无法隐藏节点,所以你必须使用其他方法,基本上是用更少的节点重新创建树。这并不难,但需要额外的 JSL 和一些参数管理。
回复:在树框中搜索元素我想通过移除节点来继续操作。每次关键字发生变化时,我都会再次移除该节点。例如,在 JSL 中,第一个关键字是 1,然后更改为 2,但最终总是会出错。这是为什么呢?按照这个逻辑,我该如何修正?
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 );
回复:在树框中搜索元素您正在删除树,并且无法访问它。不要使用“duplicate = tree”命令,而是使用克隆或其他方法复制显示框结构。您必须确保所有节点都被正确克隆/复制,并且它们不引用相同的对象(复制与深层复制)。
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
回复:在树框中搜索元素其含义似乎是,即使使用 Clone Box 方法,也无法避免当删除复制的树节点时,原始树及其根也会被删除。
这意味着每次改变关键词时,都必须建立一棵新树,然后删除与该关键词匹配的节点。因此,如果节点很多,搜索关键词的计算量就会非常大。
是否有其他方法可以创建原始树的完全独立的副本?
回复:在树框中搜索元素您可以遍历树形框并“手动”创建副本(循环遍历树并构建副本),或者基于其他数据结构(例如数据表/关联数组/您自己的自定义类)重建它。或者,您可以尝试使用表达式/函数来防止这种情况发生。
回复:在树框中搜索元素我每次似乎只能重新生成一棵树。我将其更改为以下 JSL,当关键字为 1 时,它运行正常,但如果我输入 2、4 或类似的值,它就不会成功。您能告诉我问题可能出在哪里吗?
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>
<BR />回复:在树框中搜索元素这个非常简单的例子可能会给你一些想法
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>
<P><CODE class=" language-jsl">
回复:在树框中搜索元素我尝试将其更改为以下 JSL。我希望在选择节点后按下按钮即可获取标签名称。但是,这会导致在输入关键字进行筛选后在树上选择新选项时出错,可能是因为 `new_tree` 只是一个局部变量。我应该如何修复这个问题,以便能够在关键字搜索后从新树中获取所选标签?
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>回复:在树框中搜索元素您的按钮框引用了错误的显示框,因为它所引用的树已被删除。有一些好的方法可以通过更新引用来解决这个问题,但您也可以将其设置为动态的,例如在 VLB 中始终查找最新的树框。
Button Box("Select",
label_selected = (vlb[TreeBox(1)] << get selected) << get label;
Try(ShowText << delete);
VLB2 << Append(ShowText = Text Box(label_selected));
),