问题
I want to use a counter in a loop, so each table's row has its unique id
attribute which indicates the current loop iteration. How can I access the current index? (in sample code marked as ITERATOR_HERE
)
<table id="table_id" class="display">
<thead>
<th>Key</th>
<th>Value</th>
<th></th>
</thead>
<tbody>
#{list items:paras, as:'elem' }
<tr>
<td id="ITERATOR_HERE">${elem.sendAllKey}</td>
<td>${elem.sendAllValue}</td>
<td>button</td>
</tr>
#{/list}
</tbody>
</table>
回答1:
The list tag creates a variable varname_index
containing the current iteration number. See the tag reference for more information ;)
So, you snippet becomes:
#{list items:paras, as:'elem' }
<tr>
<td id="ITERATOR_${elem_index}">${elem.sendAllKey}</td>
<td>${elem.sendAllValue}</td>
<td>button</td>
</tr>
#{/list}
来源:https://stackoverflow.com/questions/12923208/how-to-get-iterators-index-in-plays-template