webCoder
webCoder

Reputation: 2222

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.

Upvotes: 2

Views: 8213

Answers (2)

code4jhon
code4jhon

Reputation: 6034

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

Upvotes: 0

Scriptable
Scriptable

Reputation: 19750

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.

Upvotes: 5

Related Questions