Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sencha touch 2 : getting item index in itemTpl

In the documentation of XTemplate, {#} can be used to get the current array index.

When I use it in the itemTpl of an xlist, I always get 1 instead of the index:

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

always produces "Item n°1" even if my store contains several items.

Am I doing something wrong ?

like image 313
borck Avatar asked Dec 21 '22 23:12

borck


1 Answers

Note that you're using a Ext.List which fetchs data from a Ext.data.Store, not an Array, so XTemplate processes only 1 item at one time. That's why the {#} (also called xindex) always return 1.

A suggest to work-around this is to set manually the index of items in your store once it's loaded, like this: (listener for your Store)

listeners: {
  load: function(store, records){
    store.each(function(record, index){
      record.set('index', index);
    },
    store
  );
}

Hope it helps.

like image 174
Thiem Nguyen Avatar answered Mar 03 '23 01:03

Thiem Nguyen