【问题标题】:Assigning listeners to different divs inside a xtemplate - extjs 3.4将侦听器分配给 xtemplate 中的不同 div - extjs 3.4
【发布时间】:2012-12-03 00:36:23
【问题描述】:

我无法为从 XTemplate 生成的不同元素添加侦听器。

  var data = {
    users: [
        { id: 1, name: 'Ed Spencer' },
        { id: 2, name: 'Abe Elias'}
    ]
};

var store = new Ext.data.JsonStore({       
    autoLoad: true,
    data : data,
    root: 'users',
    fields: [
        {name: 'id',    type: 'int'},
        {name: 'name',  type: 'string'}
    ]
});


var template = new Ext.XTemplate(
    '<tpl for=".">',
        '<div class="holder">',
            '<div class="notclicked">foobar</div>',
            '<div class="name">{name}</div>',
            '<div class="id">{id}</div>',
        '</div>',
    '</tpl>'
);



var newPanel = new Ext.Panel({

    title: "test",
    items: new Ext.DataView({
        store: store,
        tpl: template,
        itemSelector: 'div.holder',
        emptyText: 'No foo to display'
    })
});

newPanel.render('targetDiv');

我想做的是为“name”div 上的点击创建一个“click”侦听器,为“id”div 创建一个不同的侦听器。但到目前为止,我只能为谁“持有者”div 制作一个“点击”监听器。作为扩展,我希望 div 'notclicked'... 不响应点击。我一直在反对这一点。谁能给我一个正确的方向?

我使用的是 3.4。我在 jsfiddle 上放了一个小提琴:http://jsfiddle.net/tatagatha/7SfCf/6/

【问题讨论】:

    标签: extjs extjs3 dataview listeners


    【解决方案1】:

    将'delegate'与紧密的选择器一起使用

    http://www.sencha.com/blog/event-delegation-in-sencha-touch

    看起来 3.4 还没有事件委托。 因此,您必须手动检查单击了哪个节点: 这是您选择工作的小提琴。 http://jsfiddle.net/dbrin/R29U5/

    这在数据视图上进行:

         listeners: {
                click: {
                    fn: this.onClick,
                    scope: this
                }
            },
            onClick: function(event, item, options) {
                console.log(arguments);
                if (item.className === "id") {
                    console.log("Id clicked: " + item.innerHTML);
                    alert("ID clicked: " + item.innerHTML);
                }
            }
    

    【讨论】:

    • 我使用的是 Extjs 3.4。抄袭该博客文章中的内容...我尝试使用 Ext.fly 获取参考并使用 Ext.createDelegate() 设置一个简单的函数,但没有成功。任何基于上述 jsFiddle 的建议将不胜感激。
    • 谢谢!很长一段时间以来,我一直在反对这一点,认为我可以在 DataView 创建中作为侦听器的一部分单独执行它们。看起来用 CSS 使鼠标“手”和一个 switch 语句为不同的 div 的不同行为我会得到我想要的。
    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 2014-03-29
    • 2019-12-07
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多