Get index in dart polymer repeat template
Polymer now has the ability to do this:
<template repeat="{{fruit in fruits | enumerate}}"> <li>You should try the {{fruit.value}}. It is number {{fruit.index}}.</li></template>
Example taken from https://github.com/sethladd/dart-polymer-dart-examples/blob/master/web/iterate_loop_index/my_example.html
The other solutions do not work for me in Polymer 0.3.4 (anymore?), but there is documentation on templates, including indexing while looping over a collection:
<template repeat="{{ foo, i in foos }}"> <template repeat="{{ value, j in foo }}"> {{ i }}:{{ j }}. {{ value }} </template></template>
It was available in web_ui but is not yet available in Polymer.
Web UI is the precursor to polymer.dart. Polymer.dart is almost at feature parity with Web UI. Below is a list of Web UI features that have not yet been implemented in polymer.dart:
Value of index variable available inside of loops
Until then, you can use asMap
on the list and iterate over the keys
:
<template repeat="{{i in names.asMap().keys)}}"> <div>{{i}}: {{names[i]}}</div> </template>