How to get iterator's index in Play's template?

╄→гoц情女王★ 提交于 2019-12-08 12:22:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!