如何迭代<tpl for =“。”>指定的次数?{{​​44}} {{55}} <p>即使提供的数组中有更多对象,我也需要将模板迭代两次。请帮忙。谢谢!</P> {{66}} {{11}} 35404905 {{22}} {{33}}

时间:2016-02-15 08:49:35

标签: for-loop extjs

即使提供的数组中有更多对象,我也需要将模板迭代两次。请帮忙。谢谢!

2 个答案:

答案 0 :(得分:0)

没有办法进行基于计数的迭代。相反,只需在数据中创建数组的副本:

var o = {
    items: [1, 2, 3, 4],
    foo: 1
};
var tpl = new Ext.XTemplate('{foo}<tpl for="items">{.}</tpl>');
tpl.apply(Ext.apply(o, {
    items: o.items.slice(0, 2) 
}));

答案 1 :(得分:0)

循环具有变量,其中array的项目的当前索引为xindex。所以,你可以在条件中使用循环内部的这个索引。要输出前两项列表,请检查xindex是否小于3。

    var data = {
        items: ['first', 'second', 'third', 'the last'],
        title: "The list of first 2 items: "
    };

    var tpl = new Ext.XTemplate('{title}'+
                                '<ol><tpl for="items">'+
                                '<tpl if="xindex &lt; 3">'+
                                '<li>{.}</li>'+
                                '</tpl>'+
                                '</tpl></ol>');
    tpl.append(Ext.getBody(), data);     
相关问题