javascript 如何在 extjs XTemplate 中循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8635519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop inside extjs XTemplate
提问by Gihan Lasita
Im trying to loop a array with extjs xtemplate and create a table
我试图用 extjs xtemplate 循环一个数组并创建一个表
Ext.define('dataview_model', {
extend : 'Ext.data.Model',
fields : [
{name: 'count', type: 'string'},
{name: 'maxcolumns', type: 'string'},
{name: 'maxrows', type: 'string'},
{name: 'numbers', type: 'array'}
]
});
Ext.create('Ext.data.Store', {
storeId : 'viewStore',
model : 'dataview_model',
data : [
{count: '7', maxcolumns: '10', maxrows: '5', numbers: ['100','200','300','400','500','600','700']}
]
});
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<tpl if="count > 0">',
'<table class="view_table">',
'<tpl for="numbers">',
'<tr>',
'<td>{.}</td>',
'</tr>',
'</tpl>',
'</table>',
'</tpl>',
'</tpl>'
);
Ext.create('Ext.DataView', {
width : 500,
height : 200,
renderTo : Ext.getBody(),
store : Ext.getStore('viewStore'),
tpl : tpl
});
this is the working example which i have so far
这是我迄今为止的工作示例
what i want to do is stop the loop once there is 5 rows and add other values to second column like below
我想要做的是一旦有 5 行就停止循环并将其他值添加到第二列,如下所示
+----+ +----+
| | | |
+----+ +----+
+----+ +----+
| | | |
+----+ +----+
+----+
| |
+----+
+----+
| |
+----+
+----+
| |
+----+
any idea how to do this?
知道怎么做吗?
Thanks.
谢谢。
采纳答案by Krzysztof
I can't find a way to it with template only, but below is my solution which uses template and datachanged listener.
我找不到仅使用模板的方法,但下面是我使用模板和数据更改侦听器的解决方案。
Ext.create('Ext.data.Store', {
storeId : 'viewStore',
model : 'dataview_model',
data : [
{count: '7', maxcolumns: '10', maxrows: '5', numbers: ['100','200','300','400','500','600','700']},
{count: '7', maxcolumns: '10', maxrows: '5', numbers: ['100','200','300','400','500','600','700']}
],
listeners: {
datachanged: function(store) {
store.data.each(function(record) {
record.data.groupedNumbers = [];
for (var i = 0, j = 0; i < record.data.count; ++i, j = i % record.data.maxrows) {
record.data.groupedNumbers[j] = record.data.groupedNumbers[j] || { row: j, numbers: [] };
record.data.groupedNumbers[j].numbers.push(record.data.numbers[i]);
}
});
}
}
});
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<tpl if="count > 0">',
'<table class="view_table" style="border: 1px solid black">',
'<tpl for="groupedNumbers">',
'<tr>',
'<tpl for="numbers">',
'<td>{.}</td>',
'</tpl>',
'</tr>',
'</tpl>',
'</table>',
'</tpl>',
'</tpl>'
);
Working demo: http://jsfiddle.net/6HgCd/2/
工作演示:http: //jsfiddle.net/6HgCd/2/
回答by Gihan Lasita
var tpl = new Ext.XTemplate(
'<tpl for=".">',
'<table class="view_table">',
'<tr>',
'<tpl for="numbers">',
'<td>{.}</td>',
'<tpl if="xindex % 5 === 0">',
'</tr><tr>',
'</tpl>',
'</tpl>',
'</tr>',
'</table>',
'</tpl>'
);