如何在ExtJS中使用XTemplate
How to use XTemplate in ExtJS
我在定义视图时尝试使用XTemplate,尽管我不知道我是否正确使用了它,但这是我的代码:
East.js
Ext.define('MyApp.view.main.East', {
extend: 'Ext.panel.Panel',
alias: 'widget.eastView',
requires: [
'MyApp.view.main.east.Notifications'
//'MyApp.view.main.east.Actions'
],
layout: {
type: 'vbox',
align: 'stretch'
},
border: false,
defaults: {
flex: 1,
border: false
},
items: [{
store: myStore,
tpl: notiTpl
}, {
html: 'Actions'
}]
});
Ext.define('Notifications', {
extend: 'Ext.data.Model',
fields: [
{ name:'src', type:'string' },
{ name:'from', type:'string' },
{ name:'date', type:'string' },
{ name:'content', type:'string' }
]
});
var myStore = Ext.create('Ext.data.Store', {
id:'notiStore',
model: 'Notifications',
data: [
{ src:'resources/img/east/img1.png', from:'from1', date:'24/04/2013 11:00',
content:'Bla bla bla.' },
{ src:'resources/img/east/img2.png', from:'A20132404-0002', date:'24/04/2013 10:55',
content:'Bla bla bla' }
]
});
var notiTpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="thumb-wrap">',
'<div class="notImg">',
'<img src="{src}" />',
'</div>',
'<div style="float:left; width:90%;">',
'<div>',
'<span>{from}</span>',
'<span style="float:right;">{date}</span>',
'</div>',
'<div>',
'<span>{content}</span>',
'</div>',
'</div>',
'</div>',
'</tpl>'
);
我在另一个名为Main.js
Ext.define('MyApp.view.Main' , {
extend: 'Ext.panel.Panel',
alias: 'widget.mainView',
requires: [
'MyApp.view.main.Toolbar',
'MyApp.view.main.West',
'MyApp.view.main.Center',
'MyApp.view.main.East'
],
layout: {
type: 'border',
border: false
},
defaults: {
autoScroll: true,
},
items: [{
region: 'north',
xtype: 'toolbarView'
}, {
region: 'west',
width: 250,
xtype: 'westView'
}, {
region: 'east',
width: 250,
xtype: 'eastView'
}, {
region: 'center',
xtype: 'centerView',
border: true
}]
});
有了这段代码,我只能看到工具栏的西部和中部,在东部视图中,只能看到第二个项目Actions
的html内容。我做错了什么?
另一方面,我希望我的代码整洁,我希望在商店文件夹中有商店定义,在视图文件夹中有视图,在模型文件夹中有模型,我如何从我的East视图调用这些代码部分?
提前感谢!
问候。
更新:
谢谢你的回答。这就是你说的代码,对吧?
视图中:
items: [{
xtype: 'dataview',
store: 'notiStore',
tpl: notiTpl
}, {
html: 'Actions'
}]
在Store中,更改id:'notiStore',
并写入:
storeId: 'notiStore',
这是正确的吗?我试过了,但没用,我忘了什么?
问题是我首先调用了视图。对我来说,解决方案是:
East.js
Ext.define('MyApp.view.main.East', {
extend: 'Ext.panel.Panel',
alias: 'widget.eastView',
requires: [
'MyApp.view.main.east.Notifications'
//'MyApp.view.main.east.Actions'
],
layout: {
type: 'vbox',
align: 'stretch'
},
border: false,
defaults: {
flex: 1,
border: false
},
items: [{
xtype: 'notificationsView'
}, {
html: 'Actions'
}]
});
Notifications.js
Ext.define('Notification', {
extend: 'Ext.data.Model',
fields: [
{ name:'src', type:'string' },
{ name:'from', type:'string' },
{ name:'date', type:'string' },
{ name:'content', type:'string' }
]
});
Ext.create('Ext.data.Store', {
model: 'Notification',
id: 'notiStore',
data: [
{ src:'resources/img/east/ic_new_mail_grey01.png', from:'De H24', date:'24/04/2013 11:00', content:'Recordatorio: falta sólo una hora para la generación de informes semalaes.' },
{ src:'resources/img/east/ic_to_associate_alarma_grey.png', from:'A20132404-0002', date:'24/04/2013 10:55', content:'Alerta asignada al operador MyApp' }
]
});
var notiTpl = new Ext.XTemplate(
'<tpl for=".">',
'<div class="thumb-wrap">',
'<div class="notImg">',
'<img src="{src}" />',
'</div>',
'<div style="float:left; width:90%; padding: 5px;">',
'<div>',
'<span>{from}</span>',
'<span style="float:right;">{date}</span>',
'</div>',
'<div>',
'<span>{content}</span>',
'</div>',
'</div>',
'</div>',
'</tpl>'
);
Ext.define('MyApp.view.main.east.Notificaciones', {
extend: 'Ext.view.View',
alias: 'widget.notificacionesView',
padding: 5,
store: 'notiStore',
tpl: notiTpl,
itemSelector: 'div.thumb-wrap',
emptyText: 'No images available',
renderTo: Ext.getBody()
});
可以看出,我在代码的末尾定义了视图。它工作得很好,但如果我尝试对代码进行排序,我的意思是将模型放在model文件夹中,存储在store文件夹中,并在该文件中只保留模板和视图,我将无法使其工作。有人知道如何重写我的代码来获得它吗??
问候。
您没有为第一项指定xtype
。因此,它使用defaultType
,它是"面板"。Panel不支持存储,它从其data
配置选项或使用update
方法传递的数据对象馈送。
将存储绑定到自定义XTemplate的组件是数据视图。你需要用它来代替面板。
关于你的附带问题,你可以在数据视图的文档示例中看到,你可以给你的商店一个storeId
,然后使用Ext.data.StoreManager#lookup
来检索这个商店的实例(他们在示例中使用id
而不是storeId
,但这似乎有点不推荐)。事实上,您甚至可以直接分配商店id字符串(例如store: 'myStoreId'
),Ext会很乐意为您调用StoreManager。
- ExtJS 5用程序点击actioncolumn gridview
- ExtJS——在展开/折叠时调整面板高度
- 这.SOMETHING 总是返回未定义的 - extjs
- ExtJS网格单元格编辑器,防止焦点松动问题
- 如何将本地json数据加载到Extjs数据模型中
- EXTJS 5树状网格自定义行css
- 使用js或extjs访问对象的java列表
- EXTJS-图表刷新,重绘-如何更新图表
- 类型错误:url未定义extjs 4正在填充组合框
- 事件的ExtJS控制器选择器
- 使用Extjs进行函数定义
- (extjs)获取表单中单选按钮的选定值.不返回该值
- 什么's extjs中的mon()和on()之间的区别
- 如何在ExtJS中使用XTemplate
- ExtJS 4.2.1 - 将文本字段添加到 XTemplate
- ExtJS XTemplate for loop with json
- 如何在 extjs XTemplate 中循环
- Sencha Touch / ExtJs仅在数组节点包含项时显示XTemplate
- ExtJS:如何使用itemSelector的XTemplate嵌套循环
- 如何在菜单中使用EXTJS XTemplate