Handling itemclick event on tree panel Extjs 4

36,189

Solution 1

The itemclick event listener's function param "index" does not point to your tree node's index. Like you mentioned in end of your question the syntax for the itemclick event is:

function(Ext.view.View this, Ext.data.Model record, HTMLElement item, Number index, Ext.EventObject e) {

}

Here is an example:

itemclick : function(view,rec,item,index,eventObj) {

    // You can access your node information using the record object
    // For example: record.get('id') or record.get('some-param')
    if(r.get('id')=='SP') {
        // I do my necessary logic here.. may be open a perticular window, grid etc..
    }

    if(r.get('id')=='CO') {
        // I do my necessary logic here.. may be open a perticular window, grid etc..
    }           
}

And here is an example of my tree node's data:

{ text: 'SP Reports', id: 'SP', leaf: true},
{ text: 'CO Reports', id: 'CO', leaf: true},

Solution 2

Itemclick handler already gives you everyting you need:

itemclick(view, record, item, index, e ) {
    var id = record.get('id');
    // do something depending on the record data.
    // console.log(record);
}

Solution 3

I was trying to do a generic treepanel's item click handler and to be able to get a custom field I added to the node object. This helped me out. I dunno if this is the standard and compatible ExtJs 4 way:

            (Some Panels Here),
            items: [{
                xtype: 'treepanel',
                listeners: {
                    itemclick: {

                        fn: function (view, record, item, index, e) {

                            console.log(record.raw.userData);
                        }
            (removed...)
Share:
36,189
Davor Zubak
Author by

Davor Zubak

Updated on July 09, 2022

Comments

  • Davor Zubak
    Davor Zubak almost 2 years

    what I am trying to do is to get different reaction on a different tree LEAF click!

    var myTree = Ext.create('Ext.tree.Panel',
        store: store,
        rootVisible: false,   
        border: false,
        listeners: {
            itemclick: function(index) {            
                var record = store.getAt(index);
                alert(record);          
            }
        }
    });
    

    I tried with index, to get the index of leaf, nothing. I can get a reaction on a node click, but how to get a specific reaction on every leaf? I also tried giving ID to the leafs, no luck???

    Maybe a simple example of

    itemclick: function(Ext.view.View this, Ext.data.Model record, HTMLElement item, Number index, Ext.EventObject e) {  
    
    }
    

    Pleeasse help!!

  • Admin
    Admin almost 13 years
    What about custom parameters? I can't get form record for example pareameter s even if it was send to store by json? How can I get this kid of parameters data?
  • Chris
    Chris over 12 years
    @ischenkodv best and cleanest approach in my eyes!
  • Wallysson Nunes
    Wallysson Nunes over 12 years
    What i was looking for, thanks! I was needing it: record.raw :)
  • Awalias
    Awalias over 11 years
    I also need to know how to access custom parameters
  • Awalias
    Awalias over 11 years
    I have { text: 'SP Reports', id: 'SP', leaf: true, custom: 'Hello'} how to I access the custom property?