Sencha touch 2:在itemTpl中获取项目索引

bor*_*rck 4 sencha-touch-2

在XTemplate的文档中,{#}可用于获取当前数组索引.

当我在xlist的itemTpl中使用它时,我总是得到1而不是索引:

    {
        xtype: 'list',
            store: 'myStore',
            itemTpl:new Ext.XTemplate(
                 '<tpl for=".">',
                      '<div>Item n°{#1}</div>',
                 '</tpl>'
            ),          
    }
Run Code Online (Sandbox Code Playgroud)

即使我的商店包含多个商品,也始终生成"商品编号1".

难道我做错了什么 ?

Thi*_*yen 9

请注意,您正在使用Ext.ListExt.data.Store而不是Array中获取数据,因此XTemplate一次只处理1个项目.这就是{#}(也称为xindex)总是返回1的原因.

解决此问题的建议是在加载商店后手动设置商品的索引,如下所示:(商店的监听器)

listeners: {
  load: function(store, records){
    store.each(function(record, index){
      record.set('index', index);
    },
    store
  );
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.