简体   繁体   English

EXTJS:如何在树面板的“图标”节点上添加单击事件

[英]EXTJS: How can I add an click event on Icon node in a Tree Panel

I try to get an event click on the node's Icon in a tree panel. 我尝试让事件单击树面板中的节点图标。

I have a tree with many node and in front of the leaf node, I have an Icon. 我有一棵有许多节点的树,在叶节点的前面,有一个图标。 For the moment, when I click on a node I display a PDF file. 目前,当我单击一个节点时,将显示一个PDF文件。

I want to do a specific action when I click on the Icon of this node. 当我单击该节点的图标时,我想执行特定的操作。

Someone have an idea for do that? 有人对此有想法吗?

Thanks a lot for your futur answers! 非常感谢您的未来答案!

EDIT: Thanks for your answer, 编辑:感谢您的回答,

@Hown_: Ok, but I must do an specific action which depends to the select node. @Hown_:好的,但是我必须根据选择节点执行特定的操作。 With a CSS selector, I can't do that. 使用CSS选择器,我无法做到这一点。 I'm wrong? 我错了?

@budgw: Yes, it's a good solution, but my icon must be in front of the text node :( @budgw:是的,这是一个很好的解决方案,但是我的图标必须在文本节点的前面:(

I guess the simplest way would be to add itemclick event to your TreePanel and check in the handler fn if the tree icon was clicked by checking the target of the event. 我猜最简单的方法是将itemclick事件添加到TreePanel并通过检查事件的目标来检查处理程序fn是否单击了树形图标。 It works as simple as this: 它的工作原理如下:

You may have to change the css selector of the getTarget fn for your specific use. 您可能需要针对特定​​用途更改getTarget fn的css选择器。 ( eg only leaf elements or pdf icons or something like that ) (例如,只有叶子元素或pdf图标或类似的东西)

Ext.onReady(function() {
    var store = Ext.create('Ext.data.TreeStore', {
        root: {
            expanded: true,
            children: [{
                text: "detention",
                leaf: true
            }, {
                text: "homework",
                expanded: true,
                children: [{
                    text: "book report",
                    leaf: true
                }, {
                    text: "alegrbra",
                    leaf: true
                }]
            }, {
                text: "buy lottery tickets",
                leaf: true
            }]
        }
    });    

    Ext.create('Ext.tree.Panel', {
        title: 'Simple Tree',
        width: 200,
        store: store,
        rootVisible: false,
        renderTo: Ext.getBody(),

        listeners: {
            itemclick: function(treeModel, record, item, index, e, eOpts){
                var iconElClicked = e.getTarget('.x-tree-icon');

                if(iconElClicked){
                    //tree icon clicked 

                    //do something in here
                    console.log('tree icon clicked');
                }
            }
        }

    });
});

I don't think you can do that with the icon in front of the node (maybe I'm wrong). 我认为您无法使用节点前面的图标来执行此操作(也许我错了)。

But I usually solve this kind of use case by using a treepanel with 2 columns : 但是我通常通过使用带有两列的treepanel解决这种用例:

  • a treecolumn 一个树柱
  • an action column like in this example here . 动作栏就像这个例子在这里

The trick is to use the config property 'hideHeaders:true' on the tree panel to make it looks like a classic tree. 诀窍是在树面板上使用config属性'hideHeaders:true'使其看起来像经典树。

You can select the dom elements of the icons via css selector and add a click event after the render method. 您可以通过css选择器选择图标的dom元素,并在render方法之后添加click事件。

here is an example: https://fiddle.sencha.com/#fiddle/8kd 这是一个例子: https : //fiddle.sencha.com/#fiddle/8kd

UPDATE: 更新:

exmaple: 〔实施例:

Ext.onReady(function() {
    var store = Ext.create('Ext.data.TreeStore', {
        root: {
            expanded: true,
            children: [{
                text: "detention",
                leaf: true
            }, {
                text: "homework",
                expanded: true,
                children: [{
                    text: "book report",
                    leaf: true
                }, {
                    text: "alegrbra",
                    leaf: true
                }]
            }, {
                text: "buy lottery tickets",
                leaf: true
            }]
        }
    });

    Ext.define('MyPanel', {
        extend: 'Ext.tree.Panel',
        title: 'Simple Tree',
        width: 200,

        onTreeIconClick: function(treeModel, record, item, index, e, eOpts){
            // DO SOMETHING IN HERE    
            console.log('onTreeIconClicked');
        },

        render: function(){                
            this.callParent(arguments);

            var domEls = this.el.dom.querySelectorAll('#' + this.getId() + ' .x-tree-icon', this.el.dom);

            for(var i = 0; i<domEls.length; i++){
                Ext.get(domEls[i]).on('click', function(){                    
                    //click on tree icon

                    this.on('itemclick', this.onTreeIconClick, this, { single: true });                                                     

                }, this);
            }           

        }
    });


    Ext.create('MyPanel', {

        store: store,
        rootVisible: false,
        renderTo: Ext.getBody()

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM