0

I want to search for specific node in an ExtJs tree. The current code that I have allows node to be searched only at the first level. Please check this 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);
                    }
                });
            }
        }]
    }]
});

What I want:

  1. Ability to search across all the levels in the tree
  2. once a node is found, I want to expand it.

How can I achieve this?

Thank you

2 Answers 2

7

You can use this :

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

true indicates a deep search.Please have a look at findChild.

Please check out the fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for quick reply but I am still not able to search node at level 2-3
It will search for the node at any level.It works.Only the thing is it won't expand.For that you need to check for various conditions such as; 1.a leaf node, 2.it has parent or not.
I am not sure I follow. if a node is leaf node what needs to be done and if a node is parent node what needs to be done? Please check this fiddle : jsfiddle.net/tdaXs/14 I am not able to search at lower levels.
3

This is what I was looking for : http://jsfiddle.net/tdaXs/17/

Thank you Devendra for suggesting Deep Search option.

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

1 Comment

Your fiddle is different from the code you posted above. What code is the correct one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.