-
15 Feb 2012 6:30 AM #1
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:
but it only return 1. Anyone know how to do this?Code:itemTpl: '<div>{#} - {text}</div>'
thanks
-
Best Answer Posted by Ballsacian1
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.
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.Code:itemTpl : new Ext.XTemplate('{[ this.getIndex() ]} - Yay',{ index : 0, getIndex : function(){ this.index = this.index + 1; return this.index; } });
Jordan
-
15 Feb 2012 7:29 AM #2Sencha - Senior Forum Manager
- Join Date
- Mar 2007
- Location
- St. Louis, MO
- Posts
- 33,656
- Vote Rating
- 436
- Answers
- 3108
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.
-
15 Feb 2012 7:33 AM #3
I am still using PR4, is this something that works only in the later versions?
-
22 Feb 2012 11:48 AM #4
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.
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.Code:itemTpl : new Ext.XTemplate('{[ this.getIndex() ]} - Yay',{ index : 0, getIndex : function(){ this.index = this.index + 1; return this.index; } });
Jordan
-
28 Nov 2012 9:12 AM #5
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; }
-
25 Dec 2012 12:34 AM #6
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:
Now i can use {xindex} in my itemTpl and every change in the Stores calls the refresh function to set values.Code:listeners: { refresh: function( list, data, eOpts ) { data.each( function( item, index, lenght ) { item.data.xindex = index; } ); } }
I'm new at this Sencha Development, dont think this is the best solution but it works for me.
(Sorry about my poor english)


Reply With Quote