在樹框中搜尋元素在樹狀框中搜尋元素是否可行?
以下連結中顯示的方法在列錶框中搜尋使用者輸入的元素,但此方法不適用於樹框。
在列錶框中搜尋元素
這是因為列錶框可以輕鬆使用循環來搜尋所有項目。可以使用哪些方法來搜尋樹框中的元素?
以下 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 一樣直接隱藏不匹配的子項?
回覆:在樹框中搜尋元素您是否檢查過腳本索引為 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 );
回覆:在樹框中搜尋元素您正在刪除該樹,並且將無法存取它。不要使用重複 = 樹,而是使用複製或其他方法來複製顯示框結構。您必須確保所有節點也被正確複製/複製,並且它們不引用相同的物件(複製與深度複製)。
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));
),