在extjs树商店内搜索

时间:2011-10-08 06:39:27

标签: javascript 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
    });
  }
};

3 个答案:

答案 0 :(得分:2)

filter中没有Ext.data.TreeStore方法(假设您使用的是4.x,我使用的是4.1.3)。 也许这会对你有所帮助: Ext JS 4: Filtering a TreeStore

或者,您可以尝试这样的事情:


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

希望最后一次编辑:-) 在Sencha论坛的这个主题中,他们建议您在根节点上使用CascadeBy。与上面的代码或多或少的工作方式相同。

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

答案 1 :(得分: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 :(得分:0)

我有同样的问题,我只是找到了一个小提琴来解决“在树存储中搜索”的问题。我不知道是谁写的,但这是一颗宝石。它在这里运行:https-添加冒号斜杠斜杠-fiddle.sencha.com/#fiddle/ra&view/editor (我试图将小提琴中的源代码包含在答案中,但是Stack Overflow编辑器不允许我这样做。)