1. #1
    Sencha User
    Join Date
    Dec 2011
    Posts
    40
    Vote Rating
    1
    Answers
    2
    bvkimball is on a distinguished road

      0  

    Default Answered: Is it possible to get index # in itemTpl for a list?

    Answered: Is it possible to get index # in itemTpl for a list?


    I want to use # or xindex to display in my list for example:
    Code:
    itemTpl: '<div>{#} - {text}</div>'
    but it only return 1. Anyone know how to do this?

    thanks

  2. Since itemTpl doesn't hold the loop which actually iterates over the datastore you will need to setup a custom template function to hold and retrieve this information.

    Code:
    itemTpl : new Ext.XTemplate('{[ this.getIndex() ]} - Yay',{
        index : 0,
        getIndex : function(){
             this.index = this.index + 1;
             return this.index;
        }
    });
    This has worked well for me but I still think there should be a custom property that the DataView passes into the itemTpl template for keeping track of index.

    Jordan

  3. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,656
    Vote Rating
    436
    Answers
    3108
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    You should be able to get the index by using {xindex}
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.

  4. #3
    Sencha User
    Join Date
    Dec 2011
    Posts
    40
    Vote Rating
    1
    Answers
    2
    bvkimball is on a distinguished road

      0  

    Default


    I am still using PR4, is this something that works only in the later versions?

  5. #4
    Sencha User
    Join Date
    Mar 2008
    Posts
    58
    Vote Rating
    0
    Answers
    1
    Ballsacian1 is on a distinguished road

      0  

    Default


    Since itemTpl doesn't hold the loop which actually iterates over the datastore you will need to setup a custom template function to hold and retrieve this information.

    Code:
    itemTpl : new Ext.XTemplate('{[ this.getIndex() ]} - Yay',{
        index : 0,
        getIndex : function(){
             this.index = this.index + 1;
             return this.index;
        }
    });
    This has worked well for me but I still think there should be a custom property that the DataView passes into the itemTpl template for keeping track of index.

    Jordan

  6. #5
    Sencha User
    Join Date
    Mar 2011
    Posts
    45
    Vote Rating
    1
    fdp is on a distinguished road

      0  

    Default


    For a better MVC separation it is better to override prepareData function instead of the XTemplate instance.

    to use {xindex} inside the tpl you can do this:

    Code:
    yourlist.prepareData = function(data, index, record) {
      data.xindex = index + 1;
      return data;
    }

  7. #6
    Sencha User
    Join Date
    Dec 2011
    Posts
    1
    Vote Rating
    0
    nelson.daza is on a distinguished road

      0  

    Default


    I run into this when i was using a List with the "store" property, dont know why but xindex does not exists, so i did add a listener in my Store's config, like this:

    Code:
    		listeners: {
    			refresh: function( list, data, eOpts ) {
    				data.each( function( item, index, lenght )  {
    					item.data.xindex = index;
    				} );
    			}
    		}
    Now i can use {xindex} in my itemTpl and every change in the Stores calls the refresh function to set values.
    I'm new at this Sencha Development, dont think this is the best solution but it works for me.
    (Sorry about my poor english)