首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemTpl onclick函数调用extjs 6

ItemTpl onclick函数调用extjs 6
EN

Stack Overflow用户
提问于 2017-02-17 07:08:08
回答 1查看 1.5K关注 0票数 0

如何在dataview上调用onclick函数,

代码语言:javascript
复制
itemTpl: [
'<a onClick="this.getViewController().somefunction({id},{code});" href="#">{code}</a><br/>',
]

在viewController中

代码语言:javascript
复制
somefunction: function(){
// some logic
}

给出的误差

this.getViewController()不是函数

注意:我不想做MyApp.app.getController() ,因为我有很多ViewControllers,我不能把我所有的逻辑都放在一个核心的contoller上

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-17 08:42:40

如果确实希望在itemTpl中绑定单击函数,则需要指定在模板中可访问的函数,如下所示:

代码语言:javascript
复制
// Save 'this' scope to me variable for use in XTemplate scope
var me = this;

// ...

itemTpl: [
    '<a onClick="this.someFunction();" href="#">{code}</a><br/>',
    // Defining funtions accessible in the XTemplate
    {
        someFunction: function() {
            // Use 'me' here, because 'this' is XTemplate
            me.getViewController().someFunction();
        }
    }
]

但是,如果可能的话,我将在项目上使用单击侦听器,如下所示:

代码语言:javascript
复制
// Save 'this' scope to me variable for use in the item scope
var me = this;

// ...

itemConfig: {
    listeners: {
        click: function() {
            // Use 'me' here, because 'this' is the item
            me.getViewController().someFunction();
        }
    }
}

我通常在ExtJS 4中工作,只是从ExtJS 6 文档那里得到的,所以请原谅我犯的错误并纠正我的错误,这样我就可以编辑答案了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42291521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档