简体   繁体   中英

how to attach onclick event to XTemplate element extjs?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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