2014-07-17 88 views
1

在loginHistory.scala.html文件中,我有一个List,我想按照相反的顺序进行交换。我试过这个:播放框架模板,向后遍历列表

@for(index <- historyList.size until 0) { 
    <tr class="@if(index % 2 == 0){normalRow}else{alternateRow}"> 
     <td class="col-date-time">@historyList(index).getFormattedDate</td> 
     <td class="col-result">@historyList(index).getLoginStatus</td> 
     <td class="col-protocol">@historyList(index).getProtocol</td> 
     <td class="col-ip-address">@historyList(index).getIpAddress</td> 
    </tr> 
} 

但是我最终得到一个空表。

回答

4

没有办法以相反的顺序遍历链接的List。你必须首先登录reverse。您也可以使用zipWithIndex来索引它,而不需要按索引访问元素。

@for((row, index) <- historyList.reverse.zipWithIndex) { 
    <tr class="@if(index % 2 == 0){normalRow}else{alternateRow}"> 
     <td class="col-date-time">@row.getFormattedDate</td> 
     <td class="col-result">@row.getLoginStatus</td> 
     <td class="col-protocol">@row.getProtocol</td> 
     <td class="col-ip-address">@row.getIpAddress</td> 
    </tr> 
}