在ExtJs树中搜索节点

时间:2013-09-06 13:36:31

标签: javascript extjs extjs4.1

我想在ExtJs树中搜索特定节点。我当前的代码只允许在第一级搜索节点。请查看此fiddle

var store = Ext.create('Ext.data.TreeStore', {
    root: {
        expanded: true,
        children: [{
            text: "Javascript",
            leaf: true
        }, {
            text: "ASP.net",
            leaf: true
        }, {
            text: "Also ASP.net",
            leaf: false,

            children: [{

                text: '1.1 foo',
                leaf: false,
                children: [{

                    text: "1.1.1 asp.net mvc",
                    expanded: true
                }, {

                    text: "1.1.2 java",
                    expanded: true
                }, {

                    text: "1.1.3 extjs",
                    expanded: true
                }]
            }, {

                text: '1.2 bar',
                leaf: true
            }]

        }, {
            text: "ASP.net future",
            leaf: true
        }]
    }
});

Ext.create('Ext.tree.Panel', {
    title: 'Example Tree',
    width: 200,
    height: 450,
    store: store,
    rootVisible: false,
    multiSelect: true,
    renderTo: Ext.getBody(),
    dockedItems: [{
        xtype: 'toolbar',
        dock: 'bottom',
        items: [{
            text: 'Search for ASP.net',
            handler: function () {
                var me = this,
                    panel = me.up('panel'),
                    rn = panel.getRootNode(),
                    regex = new RegExp("ASP.net");

                rn.findChildBy(function (child) {
                    var text = child.data.text;
                    if (regex.test(text) === true) {
                        console.warn("selecting child", child);
                        panel.getSelectionModel().select(child, true);
                    }
                });
            }
        }]
    }]
});

我想要的是什么:

  1. 能够搜索树中的所有级别
  2. 一旦找到一个节点,我想扩展它。
  3. 我怎样才能做到这一点?

    谢谢

2 个答案:

答案 0 :(得分:7)

您可以使用:

 var c = rn.findChild("text","Also ASP.net",true);
 c.expand();
  

true 表示深度搜索。请查看findChild

请查看fiddle

答案 1 :(得分:2)

这就是我要找的:http://jsfiddle.net/tdaXs/17/

感谢Devendra建议深度搜索选项。

 var store = Ext.create('Ext.data.TreeStore', {
    root: {
        expanded: true,
        children: [{
            text: "Javascript",
            leaf: true
        }, {
            text: "ASP.net",
            leaf: true
        }, {
            text: "Also ASP.net",
            leaf: false,
            children: [{
                text: '1.1 foo',
                leaf: false,
                children: [{
                    text: "1.1.1 ASP.net mvc",
                    leaf: true,
                    expanded: true
                }, {
                    text: "1.1.2 java",
                    leaf: true,
                    expanded: true
                }, {
                    text: "1.1.3 extjs",
                    leaf: true,
                    expanded: true
                }]
            }, {

                text: '1.2 bar',
                leaf: true
            }]

        }]
    }
});

Ext.create('Ext.tree.Panel', {
    title: 'Example Tree',
    width: 200,
    height: 450,
    store: store,
    rootVisible: false,
    multiSelect: true,
    renderTo: Ext.getBody(),
    dockedItems: [{
        xtype: 'toolbar',
        dock: 'bottom',
        items: [{
            text: 'Search for ASP.net',
            handler: function () {
                var me = this,
                    panel = me.up('panel'),
                    rn = panel.getRootNode(),
                    regex = new RegExp("ASP.net");

                //var c = rn.findChild("text", " asp.net", true);

                rn.findChildBy(function (child) {
                    var text = child.data.text;
                    if (regex.test(text) === true) {
                        console.warn("selecting child", child);
                        panel.getSelectionModel().select(child, true);
                    }
                });
            }
        }]
    }]
});