how to attach onclick event to XTemplate element extjs?

可紊 提交于 2019-12-07 20:45:12

问题


I am very new to extjs. I want to call onclick event once I click on div, Below is the code.

Please help, Thanks in advance.

var resultTemplate = new Ext.XTemplate(
    '<tpl for=".">',
    '<div class="list-item" id={value}>',
    '<i class="folder-icon">&nbsp;</i>',
    '{value}',
    '</div>',
    '</tpl>'
);


Ext.define('abc.view.xyz', {
    layout: {
        type: 'border',
        padding: 5
    },
    extend: 'Ext.Panel',
    alias: 'widget.infraTab',
    id: 'infraTab',
    margin: '10 10 10 10',
    border: true,
    items: Ext.create('Ext.view.View', {
        store: store,
        tpl: resultTemplate
    })
});

I want the div with class "list-item" clicked and the value of its id.


回答1:


You should be able to use the itemclick listener:

Ext.create('Ext.view.View', {
     store: store,
     tpl: resultTemplate,
     itemSelector: '.list-item',
     listeners: {
         itemclick: function(view, record, item, index, e, eOpts) {
             alert(record.get('value'));
         }
     }
});

Here is a Sencha Fiddle demonstrating its use.




回答2:


When you are NOT using a View then itemSelector is not available, in such cases you can do something like:

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

        this.el.select('.item', true)
            .elements
            .forEach((item) =>
                item.on('click', (e, element) => {
                        this.fireEvent('itemclick');
                    }
                )
            );
    }


来源:https://stackoverflow.com/questions/28076122/how-to-attach-onclick-event-to-xtemplate-element-extjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!