korshyadoo
korshyadoo

Reputation: 337

Play Framework template, iterate backwards over List

In the loginHistory.scala.html file, I have a List that I want to interate over in reverse order. I've tried this:

@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>
}

But I end up with an empty table.

Upvotes: 1

Views: 327

Answers (1)

Michael Zajac
Michael Zajac

Reputation: 55569

There isn't a way to traverse a linked List in reverse order. You have to reverse it first. You can also use zipWithIndex to index it, without needing to access elements by index.

@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>
}

Upvotes: 4

Related Questions