在extjs树存储中搜索

Search inside extjs tree store

本文关键字:搜索 存储 extjs      更新时间:2023-09-26

我的树工具栏中有一个文本字段,它应该从用户那里获取字符串,然后通过树的特定列进行搜索。我使用存储过滤器,但有一个问题在我的代码,我不知道它是什么。谢谢你的帮助。这是我的代码:

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
    });
  }
};

Ext.data.TreeStore中没有filter方法(假设您使用4)。x,我使用4.1.3)。也许这对你有帮助:Ext JS 4:筛选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

您可以遍历树中的所有子节点,而不是遍历商店。树的所有节点都实现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模型,那么我有一个具有teacherteacherTreeModel模型。因此,数据库中不保存教师树模型,只保存教师模型。

我有同样的问题,我刚刚找到了一个小提琴,解决了"在树存储中搜索"的问题。我不知道这是谁写的,但它是一块瑰宝。它在这里运行:https -添加冒号斜杠斜杠- fiddle.sencha.com/#fiddle/ra&view/editor(我试图包括源代码从小提琴这里到答案,但堆栈溢出编辑器不允许我。)