2011-10-08 62 views
2

我在我的樹形工具欄中有一個文本框,它應該從用戶處獲取一個字符串,然後通過特定的樹列進行搜索。我使用商店過濾器,但在我的代碼中存在問題,我不知道它是什麼。感謝幫助。 這是我的代碼:在extjs樹店內搜索

var onSimpleSearch = function(){ 
var searchStr= Ext.getCmp('searchField').getValue(); 
    if(searchStr){ 
    var tree = Ext.getCmp('infra_tree'); 
    var tstore = tree.getStore(); 
    var searchReg = new RegExp(".*" + searchStr + ".*", "ig"); 
    console.log(searchReg); //return RegExp!!!!!!! 
    tstore.filter("ipadd", searchReg}); 
}else { 
    Ext.MessageBox.show({ 
     title: 'Nothing to search', 
     msg: 'Search string is empty', 
     icon : 'ext-mb-info', 
     buttons: Ext.MessageBox.OK 
    }); 
    } 
}; 
+0

對我的問題有任何意見? –

+0

調試它。如果在filter()調用中將searchReg替換爲硬編碼值,它是否工作? –

+0

我做了,它沒有,我認爲沒有實現樹存儲過濾器,這是假的!我現在應該怎麼做?如何在商店中過濾我的數據? :( –

回答

0

你可以走了所有的子樹中的節點,而不是通過商店去。樹的所有節點都實施TreeNodeInterface,它具有您所需的方法。 cascadeBy方法可能是你正在尋找的。它會在節點的每個子節點上遞歸地調用一個函數。這基本上與商店的過濾器相同,但它知道樹的層次結構。

var searchStr = Ext.getCmp('searchField').getValue(); 
var matchingNodes = []; 

var tree = Ext.getCmp('infra_tree'); 
// You could get the selected node, or any other node as start node. 
// I take the root node as example. 
var startNode = tree.getRootNode(); 

startNode.cascadeBy(function (childNode) { 
    if (childNode.get('text') == searchStr) 
    { 
     matchingNodes.push(childNode); 
    } 
}, this); 

你可以顯然在childNode中搜索任何其他字段。通常,我在樹中保存的節點中有不同的模型。如果我有一個teacher模型顯示在一棵樹中,我有一個teacherTreeModel,它有一個teacher。因此,教師樹模型不保存在數據庫中,而僅保存在教師模型中。發現在許多情況下更容易。

2

Ext.data.TreeStore中沒有filter方法(假設您使用4.x,我使用4.1.3)。 也許這將幫助你: Ext JS 4: Filtering a TreeStore

或者,你可以嘗試這樣的事:


var nodeToSearchFor = treestore.getRootNode().findChildBy(function(node) { 
    return (node.data['fieldToSearchFor'] == valueToSearchFor); 
}); 

並希望進行最後一次編輯:-) 在這個線程在煎茶論壇上,他們建議你在根節點上使用CascadeBy。與上面的代碼大致相同。

http://www.sencha.com/forum/showthread.php?150060-Search-inside-extjs-tree-store