Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index in dart polymer repeat template

I want to iterate over an enumerable and display a counter

<div template repeat="{{ name in names }}">
  ###. {{name}}
</div>

What should I put instead of ### so it shows the position of the name:

1. John Doe
2. Jane Doe
3. etc...
like image 754
enyo Avatar asked Oct 17 '13 16:10

enyo


2 Answers

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

like image 171
Pixel Elephant Avatar answered Oct 23 '22 11:10

Pixel Elephant


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>
like image 25
Wolfram Avatar answered Oct 23 '22 11:10

Wolfram