[英]How to set custom component template and pass template arguments?
I want to create a custom component with some custom template. 我想用一些自定义模板创建一个自定义组件。 And I want this template to have some arguments.
我希望该模板具有一些参数。 Now my code looks like so:
现在我的代码如下所示:
Ext.define('Ext.ux.TestComponent', {
extend:'Ext.Component',
alias: 'widget.testcomponent',
requires: [
'Ext.XTemplate'
],
constructor: function(config) {
Ext.applyIf(config, {
tpl: [
'<div class="custom_cls" custom_attr="{custom_attr_value}">Hello world</div>',
{compiled: true}
]
});
this.callParent([config]);
}
});
Ext.create("Ext.ux.TestComponent",{
renderTo: document.getElementById("component")
});
Here is a fiddle with this code. 这是这段代码的摆弄 。 The first problem with this code is that my custom template is not rendered at all.
此代码的第一个问题是,我的自定义模板根本没有呈现。 The second problem, is that I do not know how to pass arguments to this template (and to set
custom_attr
in my example). 第二个问题是,我不知道如何将参数传递给此模板(并在示例中设置
custom_attr
)。 When I extend Button
, for example, I know I can use renderTpl
and getTemplateArgs()
and when I extend DataView
, I can configure the template inside the constructor. 例如,当我扩展
Button
,我知道可以使用renderTpl
和getTemplateArgs()
而当我扩展DataView
,可以在构造函数中配置模板。 But all this just does not work, when I extend Component
. 但是,当我扩展
Component
时,所有这些都不起作用。 So, what is wrong with that and how can I fix it? 那么,这有什么问题,我该如何解决?
You need to define your template within the renderTpl
configuration. 您需要在
renderTpl
配置中定义模板。
Additional data can be applied via renderData
. 可以通过
renderData
应用其他数据。
Ext.define('Ext.ux.TestComponent', {
extend: 'Ext.Component',
renderTpl: [
'<div class="custom_cls" custom_attr="{custom_attr_value}">Hello</div>'
],
renderData: {
custom_attr_value: "TestValue"
}
});
Here is the full example: 这是完整的示例:
https://fiddle.sencha.com/#view/editor&fiddle/21eg https://fiddle.sencha.com/#view/editor&fiddle/21eg
As an alternative to handing over the renderData
object, you can implement an initRenderData
function in your component. 作为移交
renderData
对象的替代方法,可以在组件中实现initRenderData
函数。 This function is called by the mixin Ext.util.Renderable
, which is used by Ext.Component
. 该函数由
Ext.Component
使用的mixin Ext.util.Renderable
调用。
Ext.define('Ext.ux.TestComponent', {
extend: 'Ext.Component',
renderTpl: [
'<div>{title}</div>'
],
config: {
title: 'default',
},
initRenderData() {
return this;
},
});
Ext.create("Ext.ux.TestComponent", {
renderTo: Ext.getBody(),
title: 'TestTitle'
});
Here is my full example for this alternative solution: 这是此替代解决方案的完整示例:
https://fiddle.sencha.com/#view/editor&fiddle/21en https://fiddle.sencha.com/#view/editor&fiddle/21en
tpl
is used in conjunction with the data
config. tpl
与data
配置结合使用。
Ext.define('Ext.ux.TestComponent', {
extend: 'Ext.Component',
tpl: [
'<div class="custom_cls" custom_attr="{custom_attr_value}">{content}</div>'
]
});
Ext.create("Ext.ux.TestComponent", {
renderTo: document.body,
data: {
custom_attr: 'x',
content: 'y'
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.