Looking for a quick solution?
Just add index function
var data = { items: [{ name: Aliasghar , grade: 19 }, { name: Afagh , grade: 20 }] , index: function() { return ++window['INDEX']||(window['INDEX']=0); } }
and your template might look like this:
{{
How it works
We add index: function(){} to the data, and we use it as a regular function in a template. This function adds a key to the window object, which is globally accessible; then increases it one by one.
For use with multiple lists.
Note that if you are using several templates one after another, you need to either reset window['INDEX'] or change it to something else, for example window['YEKI_DIGE'] . Another way to do this is to add a resetIndex function. Here's how to do it:
var data = { items: [{ name: Aliasghar , grade: 19 }, { name: Afagh , grade: 20 }] , index: function() { return ++window['INDEX']||(window['INDEX']=0); } , resetIndex: function() { window['INDEX']=null; return; } }
and your template might look like this:
{{
Inspired by this answer: https://stackoverflow.com/a/4158261/from-dave-in-java/index.html
Alexar Oct 30 '13 at 20:53 on 2013-10-30 20:53
source share