【问题标题】:How to loop inside extjs XTemplate如何在 extjs XTemplate 内循环
【发布时间】:2012-01-27 23:05:55
【问题描述】:

我试图用 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 &gt; 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    
});

这是我目前的工作示例

http://jsfiddle.net/6HgCd/

我想要做的是一旦有 5 行就停止循环并将其他值添加到第二列,如下所示

+----+ +----+
|    | |    |
+----+ +----+
+----+ +----+
|    | |    |
+----+ +----+
+----+
|    |
+----+
+----+
|    |
+----+
+----+
|    |
+----+

知道怎么做吗?

谢谢。

【问题讨论】:

    标签: javascript extjs extjs4


    【解决方案1】:

    我无法找到仅使用模板的方法,但下面是我使用模板和 datachanged 侦听器的解决方案。

    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 &gt; 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>'
    );
    

    工作演示:http://jsfiddle.net/6HgCd/2/

    【讨论】:

    • 非常感谢您的回答,我找到了一种仅使用模板循环虎钳列的方法
    【解决方案2】:
    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>'    
    );
    

    http://jsfiddle.net/6HgCd/4/

    【讨论】:

    • 据我所见,它创建了 5 列和 2 行,而不是 5 行和 2 列。它真的回答了这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    相关资源
    最近更新 更多